[英]asp.net mvc Anchor Tag Helpers generates html “id” is part of the query string ,not part of the route data
Startup.cs启动.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
} else {
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseCookiePolicy(new CookiePolicyOptions {
MinimumSameSitePolicy = SameSiteMode.Strict,
});
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
CustomerController.cs客户控制器.cs
URL: /customer/details/7 URL: /customer/details/7
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Details(int id) {
var model = GetUser();
return View(model);
}
After adding the following code (get the detailed information of the currently logged in customer)添加以下代码后(获取当前登录客户的详细信息)
URL: /customer/details URL:/客户/详情
[Route("[controller]/[action]")]
public async Task<IActionResult> Details() {
int userId = Convert.ToInt32(HttpContext.User.Identity.Name);
return await Details(userId);
}
Anchor Tag Helpers generates html "id" is part of the query string,not part of the route data. Anchor Tag Helpers 生成 html “id”是查询字符串的一部分,而不是路由数据的一部分。
<a asp-action="Details" asp-controller="Customer" asp-route-id="@HttpContextAccessor.HttpContext.User.Identity.Name">User Details</a>
It generates this HTML:它生成这个 HTML:
<a href="/Customer/Details?id=1">User Details</a>
How to still generates HTML:如何仍然生成 HTML:
<a href="/Customer/Details/1">User Details</a>
Thanks谢谢
Point 1: Your default routing template is第 1 点:您的默认路由模板是
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Point 2: If you put a routing directly to your controller action this will override the default routing since your two action has the same name, you need to also add a routing to the other one.第 2 点:如果您将路由直接添加到 controller 操作,这将覆盖默认路由,因为您的两个操作具有相同的名称,您还需要将路由添加到另一个操作。 Check the below code, this is.检查下面的代码,这是。
SOLUTION 1解决方案 1
[Authorize(Roles = "Admin")]
[Route("[controller]/[action]/{id}")]
public async Task<IActionResult> Details(int id) {
var model = GetUser();
return View(model);
}
[Route("[controller]/[action]")]
public async Task<IActionResult> Details() {
int userId = Convert.ToInt32(HttpContext.User.Identity.Name);
return await Details(userId);
}
Point 3: If you can change the action name of the other controller then you can go with this approach.第 3 点:如果您可以更改其他 controller 的操作名称,那么您可以使用此方法 go。 Just rename the action which has no routing directly on it.只需重命名直接没有路由的操作。
SOLUTION 2解决方案 2
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Details2(int id) {
var model = GetUser();
return View(model);
}
[Route("[controller]/[action]")]
public async Task<IActionResult> Details() {
int userId = Convert.ToInt32(HttpContext.User.Identity.Name);
return await Details(userId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.