Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Routes within WebAPI controller

Quick question regarding routes within MVC and WebAPI. I have added a route to route config.cs:

        routes.MapRoute(
            name: "ConfirmEmail",
            url: "ConfirmEmail/{userid}",
            defaults: new { controller = "Email", action = "ConfirmEmail" }
        );

This is registered in the global.asax as per normal:

RouteConfig.RegisterRoutes(RouteTable.Routes);

I am trying to generate a URL for use within an email which is sent as part of a function call within a WebAPI controller function. I am using the UrlHelper.Link function to attempt to generate a URL, however I receive an error saying the route cannot be found by name:

var url = Url.Link("ConfirmEmail", new { userid = "someUserId" });

Now I was under the impression route dictionaries were shared in both MVC and WebAPI controller contexts however I cannot see the MVC routes within the route dictionary of the incoming Web API call (on the Request object) however the WebAPI routes I have defined are there.

Am I missing something?

like image 300
Graham Whitehouse Avatar asked Mar 19 '15 14:03

Graham Whitehouse


2 Answers

Here is a much more cleaner way to generate links to MVC routes from WebApi. I use this method in a custom base api controller.

protected string MvcRoute(string routeName, object routeValues = null)
{
    return new System.Web.Mvc.UrlHelper(System.Web.HttpContext.Current.Request.RequestContext)
       .RouteUrl(routeName, routeValues, System.Web.HttpContext.Current.Request.Url.Scheme);

}
like image 141
Owre Avatar answered Sep 19 '22 13:09

Owre


Using Richards hint of where to find the routes, I have put together the following function:

    // Map an MVC route within ApiController
    private static string _MvcRouteURL(string routeName, object routeValues)
    {
        string mvcRouteUrl = "";

        // Create an HttpContextBase for the current context, used within the routing context
        HttpContextBase httpContext = new System.Web.HttpContextWrapper(HttpContext.Current);

        // Get the route data for the current request
        RouteData routeData = HttpContext.Current.Request.RequestContext.RouteData;

        // Create a new RequestContext object using the route data and context created above
        var reqContext = new System.Web.Routing.RequestContext(httpContext, routeData);

        // Create an Mvc UrlHelper using the new request context and the routes within the routing table
        var helper = new System.Web.Mvc.UrlHelper(reqContext, System.Web.Routing.RouteTable.Routes);

        // Can now use the helper to generate Url for the named route!
        mvcRouteUrl = helper.Action(routeName, null, routeValues, HttpContext.Current.Request.Url.Scheme);

        return mvcRouteUrl;
    }

It's a bit raw but did the job for me, just thought I'd put this here in case anyone else had the same problem!

like image 43
Graham Whitehouse Avatar answered Sep 20 '22 13:09

Graham Whitehouse