3

in my asp.net mvc3 web site, I use Url.Action in the view to generate url.

@Url.Action("Profile", "Users", new { @Id = Model.UserId, name = Model.NickName })

now, in one of my helper class, I need to do the same. but get this error message, "The name 'Url' does not exist in the current context.

is there another function I can use to generate url?

2
  • What is your helper class? HtmlHelper<TModel> extensions? Commented May 29, 2012 at 19:09
  • just a regular class file. not htmlhelper extension. Commented May 29, 2012 at 19:11

3 Answers 3

12

@Url in your example is an instance of UrlHelper class. You can create an instance of it and use in any class like in the example below, but you need to have an access to the RequestContext (named context in my example) of the corresponding View.

var urlHelper = new UrlHelper(context);
string absUrl = urlHelper.Action("ActionName", 
          "ControllerName", null, Request.Url.Scheme);
Sign up to request clarification or add additional context in comments.

3 Comments

-1: there is a solution using the route value dictionary which doesn't make it necessary to use the UrlHelper or to have access to the context. See my answer.
The question was how to generate the Url, not the RedirectToRouteResultinstance.
Yes, you're right. So, I've removed my downvote. (I made a "dummy edit" to be allowed to remove my downvote)
3

You can't use a Razor helper outside of a Razor view, @Url is a Razor helper and is only scoped to the view page. Not inside a .net class file.

Try this example in your class file

var urlHelper = new UrlHelper(context);
string absUrl = Url.Action("Users", "Users",  new { Id = UserId, name =.NickName }, Request.Url.Scheme);

Or from a controller just use

return RedirectToAction("Profile", "Users", new { @Id = Model.UserId, name = Model.NickName })

1 Comment

i think you have a typo on the 2nd line of your code, you should use your urlHelper variable when calling Action(), instead of the Url helper from Razor.
1

You can use this code anywhere, and you don't need the UrlHelper or the context:

RouteValueDictionary rvd = new RouteValueDictionary
{
  {"controller", "ControllerName"}, 
  {"action", "ActionName"}, 
  {"area", ""}
};

VirtualPathData virtualPathData 
   = RouteTable.Routes.GetVirtualPath(null, new RouteValueDictionary());
return virtualPathData.VirtualPath;

This will only work if the property 'HttpContext.Current' is not null, which is true for code running in a web app. If you don't supply the first parameter for GetVirtualPath the system will find use the context returned by the aforesaid static property.

So it's still true that you can use this method to get the URL without supplying the context.

The context is always necessary because it has the information of the server, and virtual directoy if present, in which the app is running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.