繁体   English   中英

使用请求URL中的命名参数进行路由

[英]Routing using a named parameter in the request URL

我正在尝试导航到/Account/UserProfile/{username}路由,但不确定是否已正确配置路由。 更确切地说,我不确定要添加到路由表中的内容以使此路由正常工作。

这是操作方法:

public IActionResult UserProfile(string username)
{
    // Do something
}

这是我正确命中的简单GET方法。 我的问题是,即使我在URL中提供了一个字符串,例如: /Account/UserProfile/MyUsername ,字符串MyUsername也没有发送到我的控制器。

创建应用程序时,我只添加了标准路由。 我需要添加什么才能使这些路由正常工作?

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

该值将位于username密钥下的RouteData中,但不会自动映射到名为username的参数,因为该密钥不是包含该参数名的已知路由值。

您可以为此方法创建一条路线:

routes.MapRoute(
    name: "userProfileByUsername",
    template: "Account/UserProfile/{username}"),
    defaults: new { controller = "Account", action = "UserProfile" });

但这将是无稽之谈,因为这将需要为每种操作方法创建一条路由。 当出现一条针对单一路线的多种用途的模式时,它会为创建这样的路线带来回报,因为它使您不必多次声明相同的属性。

例如,当您想在一个控制器中声明多个操作方法时:

  • /Account/UserProfile/{username}
  • /Account/View/{username}
  • /Account/Foo/{username}
  • /Account/Bar/{username}

然后,创建一条新路由将很明智:

routes.MapRoute(
    name: "accountActionByUsername",
    template: "Account/{action}/{username}"),
    defaults: new { controller = "Account" });

对于一次性情况,或每种操作方法的模式不同时,可以使用属性路由选择加入特定的路由:

[HttpGet("[action]/{username}"]
public IActionResult UserProfile(string username)

注意使用新的[action]占位符 ,因此您不再需要在字符串中使用动作名称。

或者,您可以通过访问原始路线数据来找到值,但实际上不应该:

public IActionResult UserProfile()
{
    string username = ViewContext.RouteData.Values["username"];

    // ...
}

最后,您可以选择将用户名作为查询字符串参数:

public IActionResult UserProfile([FromQuery]string username)

发出请求URL /Account/UserProfile?username=MyUsername

您在路线中缺少用户名参数。 参数名称需要匹配

app.UseMvc(routes => {
    routes.MapRoute(
        name: "UserProfile",
        template: "Account/UserProfile/{username}",
        defaults: { controller = "Account", action = "UserProfile" });

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

暂无
暂无

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

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