在ASP.NET Core中使用字符串参数进行路由到控制器操作

huangapple go评论51阅读模式
英文:

Routing to controller actions in ASP.NET Core with String parameter

问题

我想设置我的项目中的路由,以便我可以拥有像 /Account/Profile/FrankReynolds 这样的URL,而不是不得不使用 /Account/Profile?name=FrankReynolds。如果我尝试访问URL /Account/Profile/FrankReynolds,name参数会传递为 NULL

这是Account控制器上的IActionResult

public async Task<IActionResult> Profile(string name, string filter = null, int? page = null)
{ .... }

在我的Program.cs中,我目前正在使用以下内容:

app.MapControllerRoute(
    name: "AccountDefault",
    pattern: "{controller=Account}/{action=Profile}/{name}"
);
英文:

I want to set up the routing in my project so that I can have a URL like /Account/Profile/FrankReynolds instead of having to do /Account/Profile?name=FrankReynolds. If I try and hit the URL /Account/Profile/FrankReynolds the name parameter is coming through as NULL.

This is the IActionResult on the Account controller.

public async Task&lt;IActionResult&gt; Profile(string name, string filter = null, int? page = null)
{ .... }

In my Program.cs I am currently using the following:

app.MapControllerRoute(
    name: &quot;AccountDefault&quot;,
    pattern: &quot;{controller=Account}/{action=Profile}/{name}&quot;
);

答案1

得分: 2

如果您的控制器命名为AccountController,并且操作方法命名为Profile,您可以使用以下模式:

pattern: {controller}/{action}/{name}

如果确实无法正常工作,可以在控制器类和方法上使用属性。

我建议使用以下属性:

[ApiController]
[Route("[controller]/[action]")]
public class AccountController : ControllerBase
{
    // ...
    
    [HttpGet("{name}")]
    public async Task<IActionResult> Profile(string name, string filter = null, int? page = null)
    {
        // ...
    }
}

要使此工作正常,您可能需要在Program.cs/Startup.cs中使用默认的控制器路由,如 app.MapDefaultControllerRoute();

总的来说,我建议在控制器和操作方法上配置路由,而不是在启动过程中使其复杂化。这样可以更精细地控制每个操作以及您希望为它们配置的路由。

英文:

If your controller is named AccountController and the action method is named Profile, the pattern you can use is something like this:

pattern: {controller}/{action}/{name}

If that indeed doesn't work out, attributes on the controller class and methods should definitely work.

I suggest using the following attributes:

[ApiController]
[Route(&quot;[controller]/[action]&quot;)]
public class AccountController : ControllerBase
. . .
[HttpGet(&quot;{name}&quot;)]
public async Task&lt;IActionResult&gt; Profile(string name, string filter = null, int? page = null)

For this to work, you'll likely have to use the default controller router in your Program.cs/Startup.cs as app.MapDefaultControllerRoute();.

In general, I do suggest configuring routing on controllers and action methods instead of bloating the startup. It gives you much finer control over each of your actions and the routing you want for them.

答案2

得分: 0

我不需要回答你的问题,只提供翻译:

我不需要回答你的问题,只提供翻译:

我需要将IActionResult更新为带有HttpGet属性的版本,该属性包括URL路径和参数名称,并且还需要在name参数之前添加FromRoute属性。

[HttpGet("Account/Profile/{name}")]
public async Task<IActionResult> Profile([FromRoute] string name, string filter = null, int? page = null)
{ .... }
英文:

I had to update the IActionResult with the HttpGet attribute that included the URL path and the parameter name and also add the FromRoute attribute just before the name parameter.

[HttpGet(&quot;Account/Profile/{name}&quot;)]
public async Task&lt;IActionResult&gt; Profile([FromRoute] string name, string filter = null, int? page = null)
{ .... }

huangapple
  • 本文由 发表于 2023年7月20日 19:39:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定