我有一个简单的 ASP.NET Core Web 应用程序,其页面如下 http://localhost:5050/Posts/Create/3
.
在我的 Razor View 中 Views/Posts/Create.cshtml
,我怎样才能获得值“3”,以便我可以创建像 <a href="/Blogs/Details/3">« Back to Blog</a>
这样的链接?
到目前为止,使用以下标记帮助程序创建了链接,但我不知道要为 asp-route-id
添加什么内容:
<a asp-controller="Blogs" asp-action="Details" asp-route-id="" class="btn btn-default btn pull-right">« Back</a>
我只是使用默认的 mvc 路由:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我知道我可以从模型中获取它(即 Model.Blog.BlogId
),但我希望使用类似 Request.Query["id"]
的东西从 Url 中获取它在Views/Post/Create.cshtml
.
我试过asp-route-id="@Context.Request.Query["id"]
.
最佳答案
类似的东西Context.GetRouteData().Values["id"];
?
还有http://www.blakepell.com/how-to-get-the-current-route-in-a-view-with-asp-net-5-mvc-6
关于asp.net-core - 如何从 ASP.NET Core 中的 Razor View 获取 Url 中的 id 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42046781/