How to use Url.Action() in a class file?

xyan
xyan
78 Points
3 Posts

As we use Url.Action in the view to generate url as:

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

Now, in one of my regular class, I need to do the same i.e. generate url. Can I get this?

Views: 13335
Total Answered: 1
Total Marked As Answer: 1
Posted On: 16-Jun-2017 02:16

Share:   fb twitter linkedin
Answers
Smith
Smith
2890 Points
78 Posts
         

@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 of the corresponding View.

var urlHelper= new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext);
string absUrl = urlHelper.Action("MyProfile", "User",new { @Id = Model.UserId, name = Model.NickName }, Request.Url.Scheme);
Posted On: 16-Jun-2017 02:37
great..
 - hambi  13-Nov-2017 06:37
 Log In to Chat