【问题标题】:Route table in MVC6 for web API not workingWeb API 的 MVC6 中的路由表不起作用
【发布时间】:2015-05-27 08:28:40
【问题描述】:

我在 Startup.cs 中有以下代码:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "DefaultApi",
                template: "api/{controller=Customers}/{id?}");
        });
    }
}

... 并在 project.json 中声明 MVC 依赖项:

{
    "webroot": "wwwroot",
    "version": "1.0.0-*",
    "dependencies": {
        "Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
        "Microsoft.AspNet.Mvc": "6.0.0-beta3"
    },
    "frameworks": {
        "aspnet50": { },
        "aspnetcore50": { }
    },
    "bundleExclude": [
        "node_modules",
        "bower_components",
        "**.kproj",
        "**.user",
        "**.vspscc"
    ],
    "exclude": [
        "wwwroot",
        "node_modules",
        "bower_components"
    ]
}

我有以下控制器:

using Microsoft.AspNet.Mvc;
using System.Collections.Generic;

namespace WebApplication1.Controllers
{
    public class CustomersController : Controller
    {
        private List<Customer> _Customers = new List<Controllers.Customer>();

        public CustomersController()
        {
            _Customers.Add(new Customer() { ID = 1, Name = "Fred" });
            _Customers.Add(new Customer() { ID = 2, Name = "Bob" });
            _Customers.Add(new Customer() { ID = 3, Name = "Tim" });
        }

        public List<Customer> Get()
        {
            return _Customers;
        }

        public Customer Get(int ID)
        {
            Customer Customer = _Customers.Find(c => c.ID == ID);
            return Customer;
        }
    }

    public class Customer
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

...但是当我浏览到 /api/customers 或 /api/customers/1 我得到 404 Not Found

我错过了什么?是否支持 MVC6 中 Web API 的路由表?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-routing asp.net-core-mvc


    【解决方案1】:

    如果您尝试将您的 Asp.Net Web API 移植到 MVC 6,您需要有 Web API Compatibility Shim 。默认启动类代码说明了

     // Uncomment the following line to add Web API servcies which makes it easier to port Web API 2 controllers.
     // You need to add Microsoft.AspNet.Mvc.WebApiCompatShim package to project.json
     // services.AddWebApiConventions();
    

    默认情况下它是关闭的。启用后,您可以将 web api 路由配置为

     app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Home", action = "Index" });
    
                // Uncomment the following line to add a route for porting Web API 2 controllers.
                // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
            });
    

    更多详情here

    【讨论】:

      【解决方案2】:

      我回答这个有点晚了,但你需要把路由属性

      [Route("api/[controller]")]
      

      并从中删除 api

      template: "api/{controller=Customers}/{id?}");
      

      【讨论】:

        猜你喜欢
        • 2018-10-10
        • 2014-03-12
        • 2018-08-08
        • 2018-01-01
        • 2013-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多