Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ASP.NET Core 3.0中设置基于属性的路由_Asp.net_.net_Asp.net Mvc_Asp.net Web Api_.net Core - Fatal编程技术网

如何在ASP.NET Core 3.0中设置基于属性的路由

如何在ASP.NET Core 3.0中设置基于属性的路由,asp.net,.net,asp.net-mvc,asp.net-web-api,.net-core,Asp.net,.net,Asp.net Mvc,Asp.net Web Api,.net Core,在startup.cs课程中,我有: app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); 我的控制器当前看起来是这样的: [Microsoft.AspNetCore.Components.Route("api/my-test")] public class MyTestController : ControllerBase { [HttpGet] [R

在startup.cs课程中,我有:

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});
我的控制器当前看起来是这样的:

[Microsoft.AspNetCore.Components.Route("api/my-test")]
public class MyTestController : ControllerBase
{
    [HttpGet]
    [Route("{confirmationCode}")]
    public async Task<IActionResult> Get(string confirmationCode)
    {
        return Ok($"The confirmation code is: {confirmationCode}");
    }
}
[Microsoft.AspNetCore.Components.Route(“api/my测试”)]
公共类MyTestController:ControllerBase
{
[HttpGet]
[路由(“{confirmationCode}”)]
公共异步任务获取(字符串确认码)
{
返回Ok($“确认代码为:{confirmationCode}”);
}
}
如何调用MyTestController上的Get操作,并从如下所示的路由URL访问作为URL一部分传入的确认代码:
/api/my test/abc123

经过几次尝试,我自己找到了解决方案:

[Route("api/my-test")] // <-- this Route is from Microsoft.AspNetCore.Mvc.RouteAttribute
public class MyTestController : ControllerBase
{
    // GET api/my-test/abc123
    [HttpGet("{confirmationCode}")] // <-- instead of Route, you tack on the rest of your route pattern into the HttpGet attribute (or other verbs depending on your code)
    public async Task<IActionResult> Get(string confirmationCode)
    {
        return Ok($"The confirmation code is: {confirmationCode}");
    }
}

[Route(“api/my test”)]/经过几次尝试后,我自己找到了解决方案:

[Route("api/my-test")] // <-- this Route is from Microsoft.AspNetCore.Mvc.RouteAttribute
public class MyTestController : ControllerBase
{
    // GET api/my-test/abc123
    [HttpGet("{confirmationCode}")] // <-- instead of Route, you tack on the rest of your route pattern into the HttpGet attribute (or other verbs depending on your code)
    public async Task<IActionResult> Get(string confirmationCode)
    {
        return Ok($"The confirmation code is: {confirmationCode}");
    }
}
[路由(“api/my测试”)]//