T
T
tost842016-12-03 15:59:24
ASP.NET
tost84, 2016-12-03 15:59:24

What is the best way to do something like Url.Action in an attribute on the server side in MVC?

What is the best way to do something like Url.Action in an attribute on the server side in MVC?
Do I need to write a method that takes a UrlActionDescriptor and returns a string, or is there a better solution?
filterContext.Result = new RedirectResult(Url.Action(NameHelper.CustomName));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2016-12-03
@tost84

Redirection can be done like this:

HttpContext.Current.Response.RedirectToRoute
(
  new 
  { 
    controller = "Home", 
    action = "Index" 
  }
);

It's better to make a helper class and corresponding methods for that.
Routes can be found in System.Web.Routing.RouteTable.Routes .
Current route data:
You can instantiate UrlHelper for the context of the current request as follows:
var urlHelper = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext);
var url = urlHelper.Action("Index", "Home");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question