Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used attribute routing and set a RoutePrefix() as the default route of FirstController. Then i have created a new controller and gave its default route.

A compile time System.ArgumentException is generating on "GlobalConfiguration.Configure(WebApiConfig.Register);" in Global.asax showing Message: "A path segment cannot contain two consecutive parameters. They must be separated by a or by a literal string."

This is FirstController.cs
C#
[System.Web.Http.RoutePrefix("api/First")]
public class FirstController : ApiController
{
    [System.Web.Http.HttpGet]
    [System.Web.Http.Route("Method1")]
    public int Method1(string Param1, string Param2)
    {
      //
    }
    [System.Web.Http.HttpGet]
    [System.Web.Http.Route("Method2")]
    public int Method2(string Param1, string Param2)
    {
      //
    }
}

This is SecondController.cs
C#
[System.Web.Http.RoutePrefix("api/Second")]
     public class SecondController : ApiController
     {
         [System.Web.Http.HttpGet]
         [System.Web.Http.Route("Method3")]
         public int Method3(string Param1, string Param2)
         {
           //
         }
         [System.Web.Http.HttpGet]
         [System.Web.Http.Route("Method4")]
         public int Method4(string Param1, string Param2)
         {
           //
         }
     }

And this is my Global.asax (returning data in JSON)
C#
protected void Application_Start()
        {
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }

I'm trying to create multiple controllers having their own default routes. Please help.

What I have tried:

I've searched for the solutions but their is no specific answer regarding this problem.
I've found on answer that "As it is Web API doesn't support multiple identical attribute routes on different controllers. You will get a 404, because all the route matches more than one controller and at that point Web API will consider the result ambiguous. Note that MVC 6 does not have this limitation (but as this point it is not RTM yet).

However if you use Web API 2.11 (or newer) you can create a route constraint for the http method per controller and use it instead of the built in Route Attribute."
Posted
Updated 15-Aug-16 21:34pm

The error message shown is normally associated with a route template like
C#
Route("{Param1}{Param2}/{Param3})

where there is nothing separating two consecutive parameters like between {Param1}{Param2} in the above example. The framework will not be able to use such templates to match routes as it will not be able to identify which parameters to map to actions regardless of parameter name.

Check your route templates for typos as it is very easy to make this kind of mistake.

Try searching for }{ in your code if you cannot easily identify it on sight.
 
Share this answer
 
Add mvc route template in startup.cs file
See this
 
Share this answer
 
Comments
Priyank_Mittal 12-Aug-16 2:17am    
Concern appreciated! But their is no startup.cs file in my project.
RebelStar 16-Aug-16 2:49am    
then whatever provision(file/structure/framework/version) you have to just add template for routing with the help of internet searches.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900