Search code examples
asp.net-mvccontrollerextension-methodsurlhelper

can't access to an extension method with UrlHelper parameter in controller! Vice versa in view have access


why defined extension method with UrlHelper don't added in Url.EXTENSIONMETHOD when i want to use it in controller! but i have access to it in view?

public static string Home(this UrlHelper helper)
{
    return helper.RouteUrl("ABC", new { controller = "ABC", Action = "Default" });
}

i haven't access:

public ActionResult Default()
{
    return Redirect(Url.Home());
}

i have access in view:

<a href="<%=Url.Home() %>" title="Hello">Hello</a>

Solution

  • UrlHelper is a property of the Controller not the ControllerBase.

    Check that you have imported the namespace of the extension method class in your view. If you are importing the same namespace over and over again, you can add it in the web.config:

    <pages>
        <namespaces>
            <add namespace="System.Web.Mvc"/>
            <add namespace="System.Web.Mvc.Html"/>
            <add namespace="System.Web.Routing"/>
            <add namespace="Telerik.Web.Mvc"/>
            <add namespace="Telerik.Web.Mvc.UI"/>
            <add namespace="Shrinkr"/>
            <add namespace="Shrinkr.DataTransferObjects"/>
            <add namespace="Shrinkr.Web"/>
        </namespaces>
    </pages>