简体   繁体   中英

Routeconfig overriding WebApi Routing

I'm working on a Angular app, using ASP.NET WebApi as a backend.

http://localhost:1653/api/feed works fine, if I remove the catch-all route from the RouteConfig.cs.

When I add it back, it catches the api call also.

public class FeedController : ApiController
{
    [HttpGet]
    [Route("api/feed")]
    public IEnumerable<FeedItem> Get()
    {
        var items = new List<FeedItem>();
        items.Add(new FeedItem("News from the server!"));

        return items;
    }
}

But when removed the Angular app crashes on refresh, because the catch-all route is needed to show all of the views.

        routes.MapRoute(
            name: "Default",
            url: "{*anything}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

Shouldn't the attribute routing take care of this?

Found it:

   protected void Application_Start()
   {
        Container = UnityConfig.InitializeUnity();
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        GlobalConfiguration.Configure(WebApiConfig.Register);
        ...

it does not work if RouteConfig comes first. Ok, so there is that :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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