简体   繁体   English

相当于 Request.RequestURI 的 ASP.NET Core MVC 是什么?

[英]What is the ASP.NET Core MVC equivalent to Request.RequestURI?

I found a blog post that shows how to "shim" familiar things like HttpResponseMessage back into ASP.NET Core MVC, but I want to know what's the new native way to do the same thing as the following code in a REST Post method in a Controller:我找到了一篇博客文章,其中展示了如何将 HttpResponseMessage 之类的熟悉内容“填充”回 ASP.NET Core MVC,但我想知道在 REST Post 方法中执行与以下代码相同的操作的新本机方式是什么控制器:

// POST audit/values
[HttpPost]
public System.Net.Http.HttpResponseMessage Post([FromBody]string value)
{
    var NewEntity = _repository.InsertFromString(value);

    var msg = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Created);
    msg.Headers.Location = new Uri(Request.RequestUri + NewEntity.ID.ToString());
    return msg;

}

In an ASP.NET Core MVC project, I can't seem to get Request.RequestUri.在 ASP.NET Core MVC 项目中,我似乎无法获得 Request.RequestUri。

I tried inspecting Request, and I was able to make a function like this:我尝试检查请求,并且能够创建这样的函数:

private string UriStr(HttpRequest Request)
{
    return Request.Scheme + "://" + Request.Host + Request.Path; // Request.Path has leading /
}

So I could write UriStr(Request) instead.所以我可以改写UriStr(Request) But I'm not sure that's right.但我不确定这是对的。 I feel like I'm hacking my way around, and not using this correctly.我觉得我在绕圈子,没有正确使用它。

A related question for earlier non-Core ASP.NET MVC versions asks how to get the base url of the site.早期非核心 ASP.NET MVC 版本的相关问题询问如何获取站点的基本 url。

Personally, I use :就个人而言,我使用:

new Uri(request.GetDisplayUrl())
  • GetDisplayUrl fully un-escaped form (except for the QueryString) GetDisplayUrl完全未转义的形式(QueryString 除外)
  • GetEncodedUrl - fully escaped form suitable for use in HTTP headers GetEncodedUrl - 适用于 HTTP 标头的完全转义形式

These are extension method from the following namespace : Microsoft.AspNetCore.Http.Extensions这些是来自以下命名空间的扩展方法: Microsoft.AspNetCore.Http.Extensions

A cleaner way would be to use a UriBuilder :UriBuilder方法是使用UriBuilder

private static Uri GetUri(HttpRequest request)
{
    var builder = new UriBuilder();
    builder.Scheme = request.Scheme;
    builder.Host = request.Host.Value;
    builder.Path = request.Path;
    builder.Query = request.QueryString.ToUriComponent();
    return builder.Uri;
}

(not tested, the code might require a few adjustments) (未测试,代码可能需要一些调整)

Here's a working code.这是一个工作代码。 This is based off @Thomas Levesque answer which didn't work well when the request is from a custom port.这是基于@Thomas Levesque 的答案,当请求来自自定义端口时,该答案效果不佳。

public static class HttpRequestExtensions
{
    public static Uri ToUri(this HttpRequest request)
    {
        var hostComponents = request.Host.ToUriComponent().Split(':');

        var builder = new UriBuilder
        {
            Scheme = request.Scheme,
            Host = hostComponents[0],
            Path = request.Path,
            Query = request.QueryString.ToUriComponent()
        };

        if (hostComponents.Length == 2)
        {
            builder.Port = Convert.ToInt32(hostComponents[1]);
        }

        return builder.Uri;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM