Question

I would greatly appreciate your help in solving the following. I have setup my site to add the culture indicator into the route using the methods described in Alex Adamyan's blog Addition to ASP.NET MVC Localization - Using routing. It works great with one exception. My site uses a WebApi controller and when I make a call to an action called Upload the request is Aborted with no other errors. The issue is that in the link request the Localization route preappend the request with the culture code so that the url looks like this:

http://mysite/en/api/upload/021a6305-0e40-4a4a-a129-43c611aac371

The call to the api should not have the culture code in it and should look like this:

http://mysite/api/upload/021a6305-0e40-4a4a-a129-43c611aac371

I have tried to eliminate this by adding a route map and using the SingleCultureRouteHandeler described in the article.

Route Map Used

routes.MapRoute(
         "WebApi",
         "api/{controller}/{id}",
         new { controller = "Upload", id = UrlParameter.Optional }
         ).RouteHandler = new SingleCultureMvcRouteHandler();

I have tried the above route with and without the controller with no success.

Route Config (Just in case in missing something).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Infrastructure.Routing;

namespace MySite.MVC
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                 "Default", // Route name
                 "{controller}/{action}/{id}", // URL with parameters
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                 "WebApi",
                 "api/{controller}/{id}",
                 new { id = UrlParameter.Optional }
             ).RouteHandler = new SingleCultureMvcRouteHandler();

            foreach (Route r in routes)
            {
                if (!(r.RouteHandler is SingleCultureMvcRouteHandler))
                {
                    r.RouteHandler = new MultiCultureMvcRouteHandler();
                    r.Url = "{culture}/" + r.Url;
                    //Adding default culture 
                    if (r.Defaults == null)
                    {
                        r.Defaults = new RouteValueDictionary();
                    }
                    r.Defaults.Add("culture", Culture.en.ToString());

                    //Adding constraint for culture param
                    if (r.Constraints == null)
                    {
                        r.Constraints = new RouteValueDictionary();
                    }
                    r.Constraints.Add("culture", new CultureConstraint(Culture.en.ToString(), Culture.es.ToString(), Culture.mn.ToString()));
                }
            }

        }

    }
}

Thanks for your help in advance.

Was it helpful?

Solution

First of all, having different URL's to localize an API is a bad idea.
You should use the AcceptLanguage Header instead. URL for localization are fine for websites, but not for an API. You can find tons of discussions about this if you wanna go into this further.

routes.MapRoute(
         "WebApi",
         "api/{language}/{controller}/{id}",
         new { controller = "Upload", id = UrlParameter.Optional }
         ).RouteHandler = new MultiCultureMvcRouteHandler();

But about your route: You didn't define the culture part in your route. I think an idividual controller for ever langue is maybe overkill, so I would just go with one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top