路由模板不能以“~”开头ASP.NET 中的字符

发布于 2025-01-10 08:10:42 字数 700 浏览 0 评论 0原文

使用 Microsoft ASP.NET Core 框架和 C# 语言,我正在构建一个 Web REST 服务器,它必须响应路径以字符串“~app”开头的请求。所以,我写了这些行:

[ApiController]
[Route("~app")]
public class MyController : ControllerBase ...

但是,当执行以下行时:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

引发以下异常:

System.ArgumentException
  HResult=0x80070057
  Message=The route template cannot start with a '~' character unless followed by a '/'. (Parameter 'routeTemplate')
Inner Exception 1:
RoutePatternException: The route template cannot start with a '~' character unless followed by a '/'.

为什么请求路径中不允许使用波形符字符?

编辑:如何响应路径以字符串“~app”开头的请求?

Using the Microsoft ASP.NET Core framework and C# language, I am building a Web REST server, that must respond to requests whose path begins with the string "~app". So, I wrote these lines:

[ApiController]
[Route("~app")]
public class MyController : ControllerBase ...

Though, when the following lines are executed:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

the following exception is raised:

System.ArgumentException
  HResult=0x80070057
  Message=The route template cannot start with a '~' character unless followed by a '/'. (Parameter 'routeTemplate')
Inner Exception 1:
RoutePatternException: The route template cannot start with a '~' character unless followed by a '/'.

Why the tilde character is not allowed in the request path?

EDIT: How can I respond to requests whose path begins with the string "~app"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2025-01-17 08:10:42

开始时的 ~路由模板中的特殊字符(以及 这个)。

以下代码将 [Route("[controller]/[action]")] 应用于控制器:

[Route("[controller]/[action]")]
public class HomeController : Controller
{
    [Route("~/")]
    [Route("/Home")]
    [Route("~/Home/Index")]
    public IActionResult Index()
    {
        return ControllerContext.MyDisplayRouteInfo();
    }

    public IActionResult About()
    {
        return ControllerContext.MyDisplayRouteInfo();
    }
}

在前面的代码中,Index 方法模板必须在路由模板前面添加 /~/。应用于以 /~/ 开头的操作的路由模板不会与应用于控制器的路由模板组合

应用于以 /~/ 开头的操作的路由模板不会与应用于控制器的路由模板合并。

~ at start is special character in route templates (and this).

The following code applies [Route("[controller]/[action]")] to the controller:

[Route("[controller]/[action]")]
public class HomeController : Controller
{
    [Route("~/")]
    [Route("/Home")]
    [Route("~/Home/Index")]
    public IActionResult Index()
    {
        return ControllerContext.MyDisplayRouteInfo();
    }

    public IActionResult About()
    {
        return ControllerContext.MyDisplayRouteInfo();
    }
}

In the preceding code, the Index method templates must prepend / or ~/ to the route templates. Route templates applied to an action that begin with / or ~/ don't get combined with route templates applied to the controller

Route templates applied to an action that begin with / or ~/ don't get combined with route templates applied to the controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文