c# - 有没有办法在 ASP Net/ASP Net Core 中使用 URL 路径段中的参数

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

最近我读了一篇 article关于 URL 及其部分以及标准中定义的内容。 关于参数有一个有趣的部分。通常,您通过以下方式之一传递参数:

  • 在URL路径中作为一个段;
  • 在URL查询字符串中;
  • 在请求正文中;
  • 在请求头中;

但是,根据这篇文章,还有另一种方法 - 在 URL 路径段中传递参数,用分号将它们与段分开:

parameters – talking about parameters, these can also appear after the path but before the query string, also separated from the rest of the URL and from each other by ; characters e.g.:

http://www.blah.com/some/crazy/path.html;param1=foo;param2=bar

我以前从未遇到过这种方法,文章中提到它很少使用。 .NET 是否受支持,尤其是 ASP.NET 或 ASP.NET Core?

最佳答案

我真的不在乎这种奇怪的查询参数格式是否标准。如果您自己尝试一下,您会发现 ASP.NET Core 不支持该格式。它仍然被认为是一个段并且一旦被解析,如果它没有被任何路由模式匹配,响应只是 404 .

为了支持那种奇怪的格式(我在你的问题中谈论原始格式,而不是在你的评论中谈论另一种更奇怪的格式),理论上你可以使用定制QueryStringValueProviderFactory .默认创建 QueryStringValueProvider来自 Request.Query .在您的自定义中,您可以创建一个 QueryStringValueProvider来自您自己的一组参数,这些参数可以从原始请求 URL 中解析。然而,这种方式并不那么容易,因为您的请求在模型绑定(bind)阶段(值(value)提供者用于构建请求模型和操作参数)已经太晚了。因为为时已晚,所以您的请求路径甚至与任何路由模式都不匹配,因此管道将因 404 响应而短路。

从技术上讲,要遵循该方法,您需要以某种方式使其首先到达模型绑定(bind)阶段(这意味着使请求像分号分隔的查询参数不存在一样工作)。我认为可以删除路由过程中的最后一段(如果它包含任何分号)。然而,这当然并不容易。那种方式实在是太复杂了。

在这里,我想介绍另一种更简单(且有效)的方法。在请求进入 MVC 管道之前,我们可以使用中间件来解析 URL 并构建查询字符串(如果有的话,可能附加到现有的标准查询字符串中)。

就这么简单:

//an extension method for conveniently registering your middleware later 
public static class ComplexQueryStringMiddlewareExtensions
{
    public static IApplicationBuilder UseComplexQueryStringMiddleware(this IApplicationBuilder appBuilder)
    {
        return appBuilder.Use((context, next) => {
            var path = context.Request.Path;
            var semicolonSepParameters = path.Value.Split(';');
            //the first part is always the correct path
            context.Request.Path = semicolonSepParameters[0];
            semicolonSepParameters = semicolonSepParameters.Skip(1).Where(e => !string.IsNullOrWhiteSpace(e)).ToArray();
            if (semicolonSepParameters.Length > 0)
            {
                var appendedQueryString = string.Join("&", semicolonSepParameters);
                //in case there is some standard query string as well
                if (context.Request.Query != null && context.Request.Query.Count > 0)
                {
                    appendedQueryString = context.Request.QueryString + "&" + appendedQueryString;
                } else
                {
                    appendedQueryString = "?" + appendedQueryString;
                }
                context.Request.QueryString = new Microsoft.AspNetCore.Http.QueryString(appendedQueryString);
            }
            return next();
        });
    }
}

现在在Startup.Configure里面方法,确保你的中间件注册放在UseRouting之前(如果有的话):

app.UseComplexQueryStringMiddleware();
//if this exists (which is usually by the default generated code)
//this must be after the above
app.UseRouting();
//of course the UseEndpoints is always at the end

现在您的 http://www.blah.com/some/crazy/path.html;param1=foo;param2=bar应该像 http://www.blah.com/some/crazy/path.html?param1=foo&param2=bar 一样工作.您甚至可以将这两种格式混合在一起,例如 http://www.blah.com/some/crazy/path.html;param1=foo;param2=bar?param3=ok&param4=yes .

关于c# - 有没有办法在 ASP Net/ASP Net Core 中使用 URL 路径段中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67639030/

相关文章:

c# - 身份验证处理程序不阻止请求

c# - 如何将 Web 请求上的 Windows 身份验证 token 传递给 api?

c# - 我怎样才能做空?

c# - 如何用图像中的其他颜色替换像素颜色?

c# - 你能有一个包含破折号的属性名称吗

c# - 从 silverlight 应用程序修改数据库是个坏主意吗

asp.net - RadComboBox 中的重音不敏感搜索

javascript - 在 AspNetCore 中使用 Rotativa 将 Html 转换为 Pdf

c# - 访问基类变量不可访问错误

asp.net - 对 Azure 托管 ASP .Net 5 站点进行地理复制和流量路由,并跨大洲支持 SQL 数据库