如何从ASP.NET Core中的Razor View获取Url中id的值 [英] How to get value of id in Url from Razor View in ASP.NET Core
问题描述
我有一个简单的ASP.NET Core Web应用程序,带有类似http://localhost:5050/Posts/Create/3
的页面.
在我的Razor视图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
)中获取它,但是我希望使用Views/Post/Create.cshtml
中的Request.Query["id"]
之类的东西从网址中获取它.
我尝试了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 >
I have a simple ASP.NET Core web application with a page like such http://localhost:5050/Posts/Create/3
.
In my Razor View Views/Posts/Create.cshtml
, how would I be able to get the value "3" so that I can create a link like <a href="/Blogs/Details/3">« Back to Blog</a>
?
So far, created the link using the following Tag Helper, but I don't know what to put for asp-route-id
:
<a asp-controller="Blogs" asp-action="Details" asp-route-id="" class="btn btn-default btn pull-right">« Back</a>
I am just using the default mvc route:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
I know I can get it from the Model (i.e., Model.Blog.BlogId
), but I'm looking to get it from the Url using something like Request.Query["id"]
in Views/Post/Create.cshtml
.
I tried asp-route-id="@Context.Request.Query["id"]
.
Something like that Context.GetRouteData().Values["id"];
?
Also http://www.blakepell.com/how-to-get-the-current-route-in-a-view-with-asp-net-5-mvc-6
这篇关于如何从ASP.NET Core中的Razor View获取Url中id的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!