4

I ask a similar question here I think this is a really easy one (of course not for me). I have a extension method for Html helper and I need to get current view's url with HtmlHelper. Does anyone have any idea about it?

Community
  • 1
  • 1
Saeid
  • 13,224
  • 32
  • 107
  • 173
  • 1
    You'll have to keep in mind that a View is not the same thing as a Controller Action. MVC Routes are based on Controllers and Actions. Actions can specify any View as output. That means you can't always depend on the View name being the same as the Action. Can you give us an example of what you're trying to do with it? – Nick Bork Apr 09 '12 at 13:42
  • @Nick Bork Actually I ask a main question here: http://stackoverflow.com/questions/10069687/use-htmlhelper-to-get-action-in-beginform-method-of-asp-net-mvc-3 I need a value for action attribute in form tag and i think this value is equal by the current view's Url. – Saeid Apr 09 '12 at 14:03
  • View's don't have URLs, controller actions do. A View is just some blob of HTML. Your Actions do some CRUD on the server and then decide which blob of HTML to return. You can NEVER access a view without going through a controller action (with the exception of a PartialView nested in a View). Now, most often View names match Action names but this is not always the case. I've added a code sample for the use of a Url helper. The Url helper allows you to get what the Url for a specific route or controller action would be. That should help you populate the action tag of a form. – Nick Bork Apr 09 '12 at 14:30
  • I agree with @NickBork. Views do not have an URL per say as they are not internet reachable resource. What you may want is the URL of the original request. You should be able to extract through HttpRequestBase object through a number of methods. – Roman Apr 10 '12 at 07:38

6 Answers6

9

Based on your comment and the original thread you linked to I think you want to use a Url helper to get the URL for the form action, but I could have misunderstood what you wanted:

In a View:

 @Url.Action(null) returns the current controller/action
 @Url.Action("Action") returns a custom action with current controller
 @Url.Action("Action","Controller") returns a custom controller and action

In a HTML Helper:

 public static MvcHtmlString MySpecialHelper(this HtmlHelper htmlHelper)
 {
      UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext,htmlHelper.RouteCollection);
      string url = urlHelper.Action("Controller","Action");

      //To get the action based on the current Action/Controller use:
      url = urlHelper.Action(htmlHelper.ViewData["action"] as string);

      //or
      url = urlHelper.Action(null);

      return new MvcHtmlString(url);
 }
Koopakiller
  • 2,838
  • 3
  • 32
  • 47
Nick Bork
  • 4,831
  • 1
  • 24
  • 25
6

if you want to get information about the route information , like the controller or action method that are called

you can access the RouteData dictionary from the ViewContext Object

will be like this

@ViewContext.RouteData.Values["Controller"]

with the RouteData Dictionary you can get all the info needed about the controller , action and extra parameters' name , depending on what you want

Nadeem Khedr
  • 5,273
  • 3
  • 30
  • 37
5

If you just want the requested url then you can just get it via the Request.Url object. This returns a Uri, but if you want the raw url then Request.RawUrl.

Brendan
  • 3,415
  • 24
  • 26
2

Necromancing

public static MvcHtmlString MyHelper(this HtmlHelper htmlHelper)
{
    var url = htmlHelper.ViewContext.HttpContext.Request.Url;
    //more code
}

The OP asked about "extension method for Html helper" - so yeah, this is it.

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
0

You could use the Request.RawUrl property or Request.Url.ToString() or Request.Url.AbsoluteUri.

SoftwareNerd
  • 1,875
  • 8
  • 29
  • 58
0

You can use a Html Helper passing the object page as reference, like this:

@helper GoHome(WebViewPage page)
{
   <a href="@page.Url.Action("Index", "Home")"></a>
}

An in the View just use:

@Links.GoHome(this)
Gabriel
  • 887
  • 10
  • 22