I want to work out what the route values for the UrlReferrer in the controller action would be.
I can't figure out at what part in the MVC pipeline the incoming URL is converted into RouteValues, what I'm trying to achieve is close to that.
You need call RouteTable.Routes.GetRouteData with a mocked HttpContextBase which returns your URL in its Request.
The routes are matched internally using the request's AppRelativeCurrentExecutionFilePath.
However, this functionality is not exposed, so you need to pass an HttpContextBase.
You need to create an HttpContextBase class which returns an HttpRequestBase instance in its request property.
The HttpRequestBase class needs to return your path, beginning with ~/, in its AppRelativeCurrentExecutionFilePath property.
You don't need to implement any other properties, unless they're used by IRouteConstraints.
Someone already wrote this: Creating a RouteData instance from a URL
Related
How can I generate a URL by RouteName outside of a view or controller?
I want generate a URL by RouteName in a seperate class for an e-mail, but if I try to use UrlHelper.GenerateUrl. Then I don't see where I can get rest of the parameters for this function.
How i can generate URL by RouteName outside view or conroler ?
You shouldn't be needing/doing this. Urls should be generated only in the front layers where you have access to an HTTP context and passed to backed layers as arguments.
Of course you could always perform some horrible grotesque hack in your backend layer and hardcode some static HttpContext.Current in order to instantiate an UrlHelper and thus render your backed layers strongly coupled to the HTTP stack, making it impossible to be reused in isolation and unit tested.
Oh and by the way checkout MvcMailer if you need to send emails in your application. This way you can define the email body as templates where you will have access to helpers and stuff and won't need to perform the aformentioned grotesque hack.
I need to create a route URL based on parameters as a part of a JSON return values.
What is the equivalent of Url.RouteUrl but to be used inside the controller code,
So I can return a string in my Json result that contains the routeurl .
I need this done outside of the controller class, in a separate class, can this be done at all?
You can still use Url.RouteUrl, but in a slightly different way.
Place a using System.Web.Mvc; at the top of your class (of course, you might need to Add Reference to System.Web.Mvc).
Then get the Url object by:
UrlHelper Url = new UrlHelper(HttpContext.Current.Request.RequestContext);
and access as usual: Url.RouteUrl.
I am using quartz.net to schedule regular events within asp.net mvc application.
The scheduled job should call a service layer script that requires a UrlHelper instance (for creating Urls based on correct routes (via urlHelper.Action(..)) contained in emails that will be sent by the service).
I do not want to hardcode the links into the emails - they should be resolved using the urlhelper.
The job:
public class EvaluateRequestsJob : Quartz.IJob
{
public void Execute(JobExecutionContext context)
{
// where to get a usable urlHelper instance?
ServiceFactory.GetRequestService(urlHelper).RunEvaluation();
}
}
Please note that this is not run within the MVC pipeline. There is no current request being served, the code is run by the Quartz scheduler at defined times.
How do I get a UrlHelper instance usable on the indicated place?
If it is not possible to construct a UrlHelper, the other option I see is to make the job "self-call" a controller action by doing a HTTP request - while executing the action I will of course have a UrlHelper instance available - but this seems a little bit hacky to me.
How about just creating a new HttpContext for the UrlHelpler as in this answer:
Edit: Sorry I totally mis-read the question I guess.
It sounds like your scheduler (which I have no idea how it works) is a seperate process and you want the UrlHelper to help generate valid URLs in your MVC app?
You could try writing a handler in your MVC app that will be running under your applications context that will build the URL for you and return it. You could then call the handler from your scheduler to get any URL you need based on the params you pass in. This way your scheduler just needs to know about where the query URL of your MVC app is and then can ask it to do the Url mapping for you.
Hope this is a bit better of an answer. If I am totally off let me know... was going to delete my response but thought I would give it one more shot.
Remember to specify the protocol parameter when using UrlHelper.Action method, this will generate absolute urls. Example:
url.Action("Action", "Controller", null, "http")
or
url.Action("Action", "Controller", null, request.Url.Scheme)
You need a RequestContext to create a UrlHelper. In one of my HtmlHelper extension methods, I do it like this:
public static string ScriptUrl(this HtmlHelper html, string script)
{
var url = new UrlHelper(html.ViewContext.RequestContext);
...
}
How you get the RequestContext is dependent on your application.
Is there a class in the ASP.NET MVC framework that represents a controller name and an action name?
I'm writing a method that will return a collection of URLs and I want them to be returned as objects that contain a 'Controller' and 'Action' property or something similar.
This is because sometimes I'll need to isolate just the controller name or just the action name.
There's the Route object, but it seems way too heavyweight for what I'm trying to do.
Is there a simpler abstraction in ASP.NET MVC?
There is a Controller Base class not a name though, an action name is simply an string which the ActionInvoker will use to find the correct action via reflection.
I think you'll have to use the Route Values Dictionary of key/value pairs to represent the route.
RouteValueDictionary myValues = new RouteValueDictionary();
myValues.Add("Controller", "myController");
You could use a normal dictionary and then convert it in code to a RouteValueDictionary if you don't want/can't have to have access to the Routing namespace.
In this approach you can then using the keys of the either a standard dictionary or the routeValue Dictionary to do the isolation of the controller or action.
String ControlString = myValues["Controller"]
Then do what you want with it. Maybe use constants for the keys you wish to access.
I'm trying to URLs from the LinkBuilder in Microsoft.Web.Mvc. AT the moment, I have:
LinkBuilder.BuildUrlFromExpression(???, RouteTable.Routes,
x => x.Index())
But I'm not sure how to get the request context in all cases. E.g. If I'm in a IHttpModule, is it possible for me to somehow get the request context so I can create a URL like this?
If you are using System.Web.Routing for your MVC setup, you can implement an IRouteHandler (example) which will return a new instance of your custom IHttpHandler class. Since the logic for instantiating the HttpHandler goes inside the GetHttpHandler method of the IRouteHandler, and the IRouteHandler has access to the route request context data, you can pass that in to your HttpHandler's constructor and use it appropriately.