Reputation: 1090
Im trying to create my own helpers but im totally stuck with this error. I have searched the web and read soooo many posts about this kind of issue. When I try to add the namespace to the view I get the error "The type or namespace 'CustomHelpers' could not be found..." So I created a class for my helper and added the namespace to the Views web.config. My helper class
using System.Web.Mvc;
namespace CustomHelpers
{
public static class CustomHelpers
{
public static string Truncate(this HtmlHelper helper, string input, int length)
{
if (input.Length <= length)
{
return input;
}
else
{
return input.Substring(0, length) + "...";
}
}
}
}
My web.config
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="Playground.Web" />
<add namespace="CustomHelpers"/>
</namespaces>
</pages>
</system.web.webPages.razor>
My View
@using CustomHelpers
@{
ViewBag.Title = "Home Page";
}
Upvotes: 1
Views: 751
Reputation: 62290
The problem is you should not name a class the same as its namespace.
Do not name a class the same as its namespace
For example,
namespace YourProjectName.Framework
{
public static class CustomHelpers
{
...
}
}
Upvotes: 2