asp.net-core - 如何从 ASP.NET Core 中的 Razor View 获取 Url 中的 id 值

标签 asp.net-core asp.net-core-mvc tag-helpers

我有一个简单的 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">&laquo; 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/

相关文章:

c# - ChangeToken.OnChange 不会在自定义配置提供程序上触发

c# - 使用 Autofac 时如何解析 ASP.NET Core 中的特定键服务?

c# - RenderSection() 在 ASP.NET Core 的 <environment> 标签助手中工作吗?

c# - 将项目添加到 EF Core 中的集合导航属性

asp.net-mvc - 文件上传到 ASP。 NET MVC 6 不工作

javascript - 如何在浏览器关闭时删除cookie? ASP.NET核心,C#

c# - Entity Framework Core fluent api一对多和一对一产生重复外键

asp.net - 将QueryString附加到asp.net核心 anchor 帮助器标签中的href

asp.net-core - 在区域 View 中使用 TagHelpers

c# - ValidateAntiForgeryToken 是否使用 POST 自动实现?