英文:
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<IActionResult> Profile(string name, string filter = null, int? page = null)
{ .... }
In my Program.cs
I am currently using the following:
app.MapControllerRoute(
name: "AccountDefault",
pattern: "{controller=Account}/{action=Profile}/{name}"
);
答案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("[controller]/[action]")]
public class AccountController : ControllerBase
. . .
[HttpGet("{name}")]
public async Task<IActionResult> 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("Account/Profile/{name}")]
public async Task<IActionResult> Profile([FromRoute] string name, string filter = null, int? page = null)
{ .... }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论