简体   繁体   English

.NET Core 2.1 Web API是否支持基于约定的路由?

[英]Does .NET core 2.1 web API support convention based routing?

I am new to Web API and .net core, and I have a task to develop an API. 我是Web API和.net核心的新手,我有一个开发API的任务。

So I have created a default web API (framework: .NET Core 2.1) and tried adding the route map and i am getting an error. 因此,我创建了默认的Web API(框架:.NET Core 2.1),并尝试添加路线图,但出现错误。

guys can someone help me with routing. 伙计们可以帮助我进行路由。

Note: Cant use attribute based routing need to handle routes based on a convention like in MVC 注意:基于Cant Use属性的路由需要基于MVC之类的约定来处理路由

My startup program: 我的启动程序:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        //app.UseHttpsRedirection();
        app.UseMvc(routes =>
        {
        routes.MapRoute(
            name: "default",
            template: "{controller}/{action}/{id?}",
            defaults: new { controller = "Values", action = "dummyaction" });
        });
    }
}

And this is the error i am getting: 这是我得到的错误:

InvalidOperationException: Action 'myproject.Controllers.ValuesController.dummyaction (myproject)' does not have an attribute route. InvalidOperationException:操作'myproject.Controllers.ValuesController.dummyaction(myproject)'没有属性路由。 Action methods on controllers annotated with ApiControllerAttribute must be attribute routed. 必须对使用ApiControllerAttribute注释的控制器上的操作方法进行属性路由。

You can do with this sample 您可以使用此样本

namespace TodoApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TodoController : Controller
   {
      [HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
//do here
    }
}
}

For more information follow this link 欲了解更多信息,请点击此链接

How I got it working 我如何运作

even after using map route I was still getting the error which talks about attribute-based routing 即使在使用地图路线后,我仍然会遇到有关基于属性的路线的错误

app.UseMvc(routes =>
    {
    routes.MapRoute(
        name: "default",
        template: "{controller}/{action}/{id?}",
        defaults: new { controller = "Values", action = "dummyaction" });
    });

So in the error can you see this line "Action methods on controllers annotated with ApiControllerAttribute must be attribute routed." 因此,在错误中您可以看到以下行:“必须对使用ApiControllerAttribute注释的控制器上的操作方法进行属性路由。”

Now in my controller, I was using this particular annotation/attribute "[ApiController]" by removing that I was able to perform convention-based routing. 现在,在我的控制器中,我删除了能够执行基于约定的路由,从而使用了此特定注释/属性“ [ApiController]”。

also, I have updated the route as below 另外,我还更新了如下路线

app.UseMvc(routes =>
{
     routes.MapRoute(
            name: "api",
            template: "api/{controller=Values}/{action=gogogo}/{id?}");
 });

references: 参考资料:

https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2#mixed-routing-attribute-routing-vs-conventional-routing section(Mixed routing: Attribute routing vs conventional routing) https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2#mixed-routing-attribute-routing-vs-conventional-routing部分(混合路由:属性路由)与常规路由相比)

asp.net core web api center routing asp.net核心Web API中心路由

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

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