我正在尝试将一些 View 类从 .NET 迁移到 .NET Core,但遇到了缺少“Request”类方法的问题。
我的具体代码如下:
<form id="contactUs" method="post" action="@Request.Url.GetLeftPart(UriPartial.Authority)@Url.Action("ContactUsFormSubmit")" accept-charset="utf-8">
如何替换 .NET Core 语法的 Request.Url.GetLeftPart(UriPartial.Authority)
部分?
.NET Core 中是否有更好的调用权限部分的方法?
最佳答案
现在可以在 Controller (来自基类)或 @Context.Request
中以 Request
形式找到 Request
(如果您是)在 MVC View 中。
但是没有 ,所以你需要自己制作:(参见编辑)Request.Url
的替代品
string.Concat(Request.Scheme,
"://",
Request.Host.ToUriComponent(),
Request.PathBase.ToUriComponent(),
Request.Path.ToUriComponent(),
Request.QueryString.ToUriComponent())
但就你的情况而言,这似乎没有必要。您可以让浏览器处理相对路径...
<form id="contactUs" method="post" action="@Url.Action("ContactUsFormSubmit")" accept-charset="utf-8">
或者使用 MVC Core 原生方式(使用标签助手):
<form asp-controller="MyController" asp-action="ContactUsFormSubmit" method="post">
编辑:实际上有一个 Request.Url
的替代品:
添加:使用 Microsoft.AspNetCore.Http.Extensions;
然后调用 Request.GetDisplayUrl();
... 这将返回 https://localhost/MyController/MyAction?Param1=blah
。
关于c# - 如何替换 ASP.NET Core View 类的 "Request"类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38938058/