Call UrlHelper in models in ASP.NET MVC?

Helpful tip, in any ASP. NET application, you can get a reference of the current HttpContext HttpContext. Current which is derived from System.Web.

Therefore, the following will work anywhere in an ASP. NET MVC application: UrlHelper url = new UrlHelper(HttpContext.Current.Request. RequestContext); url.

Action("ContactUs"); // Will output the proper link according to routing info Example: public class MyModel { public int ID { get; private set; } public string Link { get { UrlHelper url = new UrlHelper(HttpContext.Current.Request. RequestContext); return url. Action("ViewAction", "MyModelController", new { id = this.ID }); } } public MyModel(int id) { this.

ID = id; } } Calling the Link property on a created MyModel object will return the valid Url to view the Model based on the routing in Global.asax.

Are you sure there's a HttpContext.Current.Request. RequestContext? HttpContext.Current.

Request seems not to have a RequestContext. – J. Pablo Fernández Jan 9 '10 at 3:07 Thats odd.

I just tested this solution out and it works perfectly. I'm running ASP. NET MVC 2 Preview 2, but I think this works across all versions.

Not sure why it's not working for you. Are you creating the class outside of an MVC project? Also make sure there are using for both System.

Web and System.Web. Mvc – Omar Jan 9 '10 at 3:18 I'm on an ASP. NET MVC 1 project, I thought about missing usings but I have both of them.

– J. Pablo Fernández Jan 9 '10 at 3:20 Not really sure why it's not showing. If anyone else could confirm this doesn't exist in ASP.NET MVC 1 that would be great.

I only have one machine with VS2010 and MVC 2 installed. If you're interested, MVC RC 2 haacked.Com/archive/2009/12/16/aspnetmvc-2-rc. Aspx – Omar Jan 9 '10 at 3:36 1 Note that Request.

RequestContex is supported in . NET4+ – h--n Jan 9 '107 at 13:31.

I like Baddie's answer but that's not working for me. Just for the record this is the solution I'm using now: var httpContext = HttpContext. Current; if (httpContext == null) { var request = new HttpRequest("/", "example.com", ""); var response = new HttpResponse(new StringWriter()); httpContext = new HttpContext(request, response); } var httpContextBase = new HttpContextWrapper(httpContext); var routeData = new RouteData(); var requestContext = new RequestContext(httpContextBase, routeData); return new UrlHelper(requestContext).

What is Config. Url? – joshcomley Jan 29 '10 at 18:14 It contains the URL of my site.

There, I removed it. – J. Pablo Fernández Feb 7 '10 at 14:29.

A UrlHelper can be constructed from within a Controller action with the following: var url = new UrlHelper(this. ControllerContext. RequestContext); url.

Action(...); Outside of a controller, a UrlHelper can be constructed by creating a RequestContext from the RouteTable. Routes RouteData. HttpContextWrapper httpContextWrapper = new HttpContextWrapper(System.Web.HttpContext.

Current); UrlHelper urlHelper = new UrlHelper(new RequestContext(httpContextWrapper, RouteTable.Routes. GetRouteData(httpContextWrapper))); (Based on Brian's answer, with a minor code correction added. ).

But I don't have a controller in the model. – J. Pablo Fernández Jan 9 '10 at 2:06 Okay I apologize, I wasn't sure exactly where the code was being executed.

Let me take a look... – Nathan Taylor Jan 9 '10 at 2:07.

Yes, you can instantiate it. You can do something like: var ctx = new HttpContextWrapper(HttpContext. Current); UrlHelper helper = new UrlHelper( new RequestContext(ctx, RouteTable.Routes.

GetRouteData(ctx)); RouteTable. Routes is a static property, so you should be OK there; to get a HttpContextBase reference, HttpContextWrapper takes a reference to HttpContext, and HttpContext delivers that.

This will not work, though it's very close. See my answer below. – Nathan Taylor Jan 9 '10 at 2:35.

I think what you're looking for is this: Url. Action("ActionName", "ControllerName").

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions