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

StackOverflow https://stackoverflow.com/questions/2587198

문제

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>
도움이 되었습니까?

해결책

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>

다른 팁

Url is a member of ViewPage, but not of ControllerBase. Therefore, you don't have a pre-constructed UrlHelper available to constructors. In general, you shouldn't need to generate URLs in your controller.

Why don't you use Controller.RedirectToRoute?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top