属性路由不适用于两个或多个控制器 [英] Attribute routing not working with two or more controllers

查看:84
本文介绍了属性路由不适用于两个或多个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了属性路由并将RoutePrefix()设置为FirstController的默认路由。然后我创建了一个新的控制器并给出了它的默认路由。



编译时System.ArgumentException正在GlobalConfiguration.Configure(WebApiConfig.Register);上生成。在Global.asax中显示消息:一个路径段不能包含两个连续的参数。它们必须用一个或一个文字字符串分隔。



这是FirstController。 cs

 [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。路线( Method2)]
public int Method2( string Param1, string Param2)
{
//
}
}



这是SecondController.cs

 [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)
{
//
}
}



这是我的Global.asax(以JSON格式返回数据)

  protected   void  Application_Start()
{
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.Refe renceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
GlobalConfiguration.Configure(WebApiConfig.Register);
}



我正在尝试创建具有自己的默认路由的多个控制器。请帮忙。



我尝试过:



我是搜索解决方案,但他们没有关于这个问题的具体答案。

我发现答案是因为它是Web API不支持不同控制器上的多个相同的属性路由。你会得到的404,因为所有路由都匹配多个控制器,此时Web API会认为结果不明确。请注意,MVC 6没有此限制(但此时它还不是RTM)。



但是如果你使用Web API 2.11(或更新版本),你可以为每个控制器的http方法创建一个路由约束,并使用它而不是内置的路由属性。

解决方案

显示的错误消息通常与路线模板相关联,例如

路线(  {Param1} {Param2} / {Param3}) 



其中在上面的例子中,re没有将两个连续参数分开,例如{Param1} {Param2}之间。框架将无法使用此类模板来匹配路由,因为无论参数名称如何,它都无法识别要映射到操作的参数。



检查您的打字错误的路线模板很容易造成这种错误。



如果您无法在视线中轻易识别,请尝试在代码中搜索} {。 / blockquote>

在startup.cs文件中添加mvc路由模板

看到这个


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

[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

[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)

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."

解决方案

The error message shown is normally associated with a route template like

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.


Add mvc route template in startup.cs file
See this


这篇关于属性路由不适用于两个或多个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆