Question

I am developping a WEB API using asp.net and my goal is to be able to call this type of url's:

/html/countries/...
/json/countries/...

Countries is a controller, and depending on the parameter before it returns different result.


What i did and it doesn't seems to work:

       routes.MapRoute(
            name: "Default",
            url: "api/{action}/{controller}",
            defaults: new
            {
            }
        );

CountriesController:

    [ActionName("html")]
    public string get()
    {
        //...
    }

    [ActionName("json")]
    public void getType()
    {
        //...
    }

Any sugestions? EDIT: I have like 7 controllers.

And there are some possible urls:

/html/{controller}/x/y
/json/{controller}/x/y/order/h
/html/{controller}/x/z/order/y/j
/json/{controller}/x/z/order/y/j
Was it helpful?

Solution 2

Thanks to the Dalorzo answer i did find the problem:

The problem occurred because my application was created the following way:

Creating my new app

which resulted in creation of both files, RouteConfig.cs (MVC) and WebApiConfig.cs (WEB API):

Both Files

What is WRONG is that the code in the question is from RouteConfig.cs

After putting the code

       config.Routes.MapHttpRoute(
            name: "Default",
            routeTemplate: "api/{action}/{controller}",
            defaults: new
            {
                action = "html"
            }
        );

in WebApiConfig.cs, it worked proper way.

OTHER TIPS

Allow me by start saying that if "html" or json action means "format" those should not be part of your controller they are media types and needs to be configured differently

Web Api v1 defines resources globally in the global.asax on application_start event. Assuming you are using Visual Studio 2013 and based Microsoft default template your method may look like this:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

WebApi routing configuration occurs here WebApiConfig.Register while your MVC configuration occurs here RouteConfig.RegisterRoutes

Your WebApi routing configuration should look like this

public static class WebApiConfig{
        public static void Register(HttpConfiguration config){            
            config.Routes.MapHttpRoute(
                name: "htmltApi",
                routeTemplate: "html/{action}/{controller}",
            );

            config.Routes.MapHttpRoute(
                name: "jsonApi",
                routeTemplate: "json/{action}/{controller}",
            );
     ...

Another important detail is that WebApi v2 introduced something called Route Attributes those can be used along with your Controller class and can facilitate the routing configuration.

For example:

 public class BookController : ApiController{
     //where author is a letter(a-Z) with a minimum of 5 character and 10 max.      
    [Route("html/{id}/{newAuthor:alpha:length(5,10)}")]
    public Book Get(int id, string newAuthor){
        return new Book() { Title = "SQL Server 2012 id= " + id, Author = "Adrian & " + newAuthor };
    }

   [Route("json/{id}/{newAuthor:alpha:length(5,10)}/{title}")]
   public Book Get(int id, string newAuthor, string title){
       return new Book() { Title = "SQL Server 2012 id= " + id, Author = "Adrian & " + newAuthor };
   }
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top