Lets say i`m in an action method and i want to generate a string like this :
http://www.myhost.com/Home/Index?Id=1
i want to save this to DB so i was wondering if there is any formal way to generate it instead of building it up myself.
I`m using MVC3
thanks in advance.
UrlHelper u = new UrlHelper(this. ControllerContext. RequestContext); string url = u. Action("About", "Home", null);
RouteUrl(String, RouteValueDictionary, String, String) Generates a fully qualified URL for the specified route values by using the specified route name, protocol to use, and host name.
There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
URL patterns for routes in MVC Applications typically include {controller} and {action} placeholders. When a request is received, it is routed to the UrlRoutingModule object and then to the MvcHandler HTTP handler.
You could use the Url property of the controller:
public ActionResult Foo()
{
string url = Url.Action("Index", "Home", new { id = 1 });
// TODO: save to DB
}
and if you need an absolute url just use the proper overload:
string url = Url.Action("Index", "Home", new { id = 1 }, "http");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With