How do I configure routing for MVC and SignalR in an ASP.NET Core project?

不想你离开。 提交于 2019-12-10 16:00:05

问题


I'm trying to build a web app with ASP.NET Core 2.1.0-preview1 and ASP.NET Core SignalR 1.0.0-alpha1 with an Angular4 client. So far I've been able to muddle through and can actually open a SignalR web socket connection from within the client. That came at a price, though, here's an excerpt of my Startup.cs:

app.UseMvc(routes =>
{
   routes.MapRoute(
      name: "default",
      template: "{controller=Home}/{action=Index}/{id?}");

   /*routes.MapSpaFallbackRoute(
      name: "spa-fallback",
      defaults: new { controller = "Home", action = "Index" });*/
});

app.UseWebSockets();
app.UseSignalR(routes =>
{
   routes.MapHub<Feed>("feed");
});

As you can see, I had to disable the "spa-fallback" route, if I didn't do that SignalR would receive plain HTML instead of JSON. The drawback of that, of course, is that I have to enter the exact URL (localhost:12345/home/index, for example) now, which sucks.

So my question is, how can I make MVC routing and SignalR routing live side by side?


回答1:


You need to put UseSignalR before UseMvc otherwise the fallback route will be registered before routes added by UseSignalR.



来源:https://stackoverflow.com/questions/46055779/how-do-i-configure-routing-for-mvc-and-signalr-in-an-asp-net-core-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!