简体   繁体   English

路线开始处的可选参数

[英]Optional parameters at the beginning of route

I'm trying to create a route that satisfies below two scenarios: 我正在尝试创建一种满足以下两种情况的路线:

  1. en-us/mycontroller/action/1 zh-cn / mycontroller / action / 1
  2. mycontroller/action/1 mycontroller / action / 1

Basically what I need is the below rule. 基本上我需要的是以下规则。 But it seems that the system is always expecting the locale parameter, even though it's optional. 但是似乎系统总是希望使用locale参数,即使它是可选参数也是如此。

[Route("{locale?}/[controller]/[action]")]
public class MyController : Controller
{
     [Route("{id}")]
     public IActionResult Action(int id) {
          return View();
     }
}

Any idea how I can create a rule that supports above scenarios? 知道如何创建支持上述方案的规则吗?

Optional parameters in routes must be at the end. 路由中的可选参数必须在末尾。 There's no way around that. 没有办法解决。 However, there's an alternate way to handle it: just add multiple routes: 但是,还有另一种处理方式:只需添加多个路由:

[Route("{locale}/[controller]/[action]")]
[Route("[controller]/[action]")]

Now it will respond to either way. 现在它将以任何一种方式响应。 FWIW, you might want to consider adding some constraints to the locale param, though, so it doesn't match everything . FWIW,尽管如此,您可能要考虑向locale参数添加一些约束,因此它与所有内容都不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM