c# - 如何替换 ASP.NET Core View 类的 "Request"类方法?

标签 c# asp.net .net asp.net-core .net-core

我正在尝试将一些 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/

相关文章:

.net - 使用 Entity Framework 按时间戳列选择新记录

c# - Process.GetProcessById 启用 "debug mode"

c# - 找不到网络路径。提供程序 : Named Pipes Provider, 错误:40 - 无法打开与 SQL Server 的连接

c# - 如何在 Windows 中检索任何给定文件类型的默认图标?

c# - HtmlAgilityPack 可以处理一个 xsl 文件自带的 xml 文件来渲染 html 吗?

C# 在 session 变量中存储替代文本或从数据库检索

asp.net - 使用 asp.net 的文本文件没有换行符

c# - 遍历Expression,找到MethodCallExpression并替换为新的Expression

asp.net - 如何与支持 ajax 的 Web 服务共享我的 asp.net 身份

.net 动态程序集