如何以 /Controller/Action/Id/.../QueryString 的形式在 Razor 视图中获取 URL
How to get URL in Razor View in the form /Controller/Action/Id/.../QueryString
我有一个表单将当前 url 传递给控制器操作,以便它将用户重定向回 he/she 之前的页面。
目前,我正在按如下方式连接 Controller、Action 和 QueryString;
var currentUrl = $"/{ViewContext.RouteData.Values["controller"]}/{ViewContext.RouteData.Values["action"]}/{Context.Request.QueryString}";
在我的控制器操作中,我有以下内容;
if (Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
我觉得这好像很死板,可能已经有一种方法可以做到这一点,但我不知道它是什么。我使用的方法不适用于 /Home/Index/1/2019/...?message=hello
.
这样的实例
你可以试试加参数routing.Here是个demo:
startup(你的参数(/1/2019/...
)的数量需要小于等于/{id?}/{id1?}/{id2?}/...
的数量):
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}/{id1?}/{id2?}");
});
查看:
<h1>
CurrentUrl:@Context.Request.Path@Context.Request.QueryString
</h1>
结果:
我有一个表单将当前 url 传递给控制器操作,以便它将用户重定向回 he/she 之前的页面。
目前,我正在按如下方式连接 Controller、Action 和 QueryString;
var currentUrl = $"/{ViewContext.RouteData.Values["controller"]}/{ViewContext.RouteData.Values["action"]}/{Context.Request.QueryString}";
在我的控制器操作中,我有以下内容;
if (Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
我觉得这好像很死板,可能已经有一种方法可以做到这一点,但我不知道它是什么。我使用的方法不适用于 /Home/Index/1/2019/...?message=hello
.
你可以试试加参数routing.Here是个demo:
startup(你的参数(/1/2019/...
)的数量需要小于等于/{id?}/{id1?}/{id2?}/...
的数量):
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}/{id1?}/{id2?}");
});
查看:
<h1>
CurrentUrl:@Context.Request.Path@Context.Request.QueryString
</h1>
结果: