views:

89

answers:

2

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>
+1  A: 

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?

kevingessner
oh! but according to rashid's post we can access it in controller!http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx#urlHelperRoute
Sadegh
+1  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>
kazimanzurrashid