如何在ASP.NET Core项目中为MVC和SignalR配置路由? [英] How do I configure routing for MVC and SignalR in an ASP.NET Core project?

查看:70
本文介绍了如何在ASP.NET Core项目中为MVC和SignalR配置路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有Angular4客户端的ASP.NET Core 2.1.0-preview1和ASP.NET Core SignalR 1.0.0-alpha1构建一个Web应用程序.到目前为止,我已经能够弄清楚,并且实际上可以从客户端内部打开SignalR Web套接字连接.不过,这是有代价的,这是我的 Startup.cs 的摘录:

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");
});

如您所见,我不得不禁用"spa-fallback"路由,如果不这样做的话,SignalR将接收纯HTML而不是JSON.当然,这样做的缺点是我现在必须输入准确的URL(例如, localhost:12345/home/index ).

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.

所以我的问题是,如何使MVC路由和SignalR路由并存?

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

推荐答案

您需要在 UseMvc 之前放置 UseSignalR ,否则,将在 UseSignalR .

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

这篇关于如何在ASP.NET Core项目中为MVC和SignalR配置路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆