没有尾部斜杠的乌里基地 [英] Base Uri without a trailing slash

查看:95
本文介绍了没有尾部斜杠的乌里基地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 UriBuilder 来创建 Uri

var rootUrl = new UriBuilder("http", "example.com", 50000).Uri;

然后是<$ c $的 AbsoluteUri c> rootUrl 始终包含以下斜杠:

then the AbsoluteUri of rootUrl always contain a trailing slash like this:

http://example.com:50000/

我想创建一个 Uri 对象没有尾部的斜杠,但这似乎是不可能的。

What I would like, is to create a Uri object without the trailing slash, but it seems impossible.

我的解决方法是将其存储为字符串,然后执行类似以下操作:

My workaround is to store it as a string instead, and do something ugly like this:

var rootUrl = new UriBuilder("http", "example.com", 50000).Uri.ToString().TrimEnd('/');

我听说人们说没有尾随的斜杠,Uri是无效的。我认为那不是真的。我查看了RFC 3986,并在3.2.2节中说:

I have heard people say that without the trailing slash, the Uri is invalid. I don't think that is true. I have looked through RFC 3986, and in section 3.2.2 it says:


如果URI包含授权组件,则路径组件
必须为空或以斜杠( /)开头。

If a URI contains an authority component, then the path component must either be empty or begin with a slash ("/") character.

这并不是说

推荐答案

在任意URI中不需要 都是必需的,但这是请求规范表示形式的一部分。 nofollow noreferrer> HTTP中的

The trailing slash is not required in an arbitrary URI, but it is the part of the canonical representation of an absolute URI for requests in HTTP:


请注意,绝对路径不能为空;如果原始URI中没有任何内容,则必须将其指定为 /(服务器根目录)。

Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as "/" (the server root).

要遵守< a href = https://tools.ietf.org/html/rfc3986#section-6.2.3 rel = nofollow noreferrer>规范,即 Uri 类以带斜杠的形式输出URI:

To adhere to the spec, the Uri class outputs a URI in the form with a trailing slash:


通常,使用授权的通用语法的URI具有空路径应归一化为路径 /。

In general, a URI that uses the generic syntax for authority with an empty path should be normalized to a path of "/".

此行为在上不可配置.NET中的Uri 对象。发送对空路径的URL的请求时,Web浏览器和许多HTTP客户端执行相同的重写。

This behavior is not configurable on a Uri object in .NET. Web browsers and many HTTP clients perform the same rewriting when sending requests for URLs with an empty path.

如果我们想在内部将URL表示为 Uri 对象,而不是字符串,我们可以创建扩展方法,用于格式化网址而不会在结尾加上斜杠,从而将该表示逻辑抽象到一个位置,而不是每次我们需要输出显示的URL时都将其复制:

If we want to internally represent our URL as a Uri object, not a string, we can create an extension method that formats the URL without the trailing slash, which abstracts this presentation logic in one location instead of duplicating it every time we need to output the URL for display:

namespace Example.App.CustomExtensions 
{
    public static class UriExtensions 
    {
        public static string ToRootHttpUriString(this Uri uri) 
        {
            if (!uri.IsHttp()) 
            {
                throw new InvalidOperationException(...);
            }

            return uri.Scheme + "://" + uri.Authority;
        }

        public static bool IsHttp(this Uri uri) 
        {
            return uri.Scheme == "http" || uri.Scheme == "https";
        }
    }
}

然后:

using Example.App.CustomExtensions;
...

var rootUrl = new UriBuilder("http", "example.com", 50000).Uri; 
Console.WriteLine(rootUrl.ToRootHttpUriString()); // "http://example.com:50000"

这篇关于没有尾部斜杠的乌里基地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆