简体   繁体   English

运行Angular 2/4路由而不是.net Core路由

[英]Running Angular 2/4 Route instead of .net Core route

in my Angular Module I have the following Route: 在我的Angular模块中,我有以下路线:

 { path: "something/:id", component: SomeComponent }

which runs fine when using "routerLinks" frontend, but as soon as I manually write the ID in the URL and press Enter, It runs the C# backend method instead and ignores the angular route. 在使用“ routerLinks”前端时可以正常运行,但是当我在URL中手动写入ID并按Enter时,它将立即运行C#后端方法,并忽略角度路线。

[HttpGet("{myid:guid}")]

The backend method returns Json as it should but I cant get my Angular route to display my component, seeing I go straight to the Controller. 后端方法按原样返回Json,但是我直接进入控制器,却无法获得Angular路线来显示组件。

So my question is: How do I tell my application to run my frontend route instead of my backend route, or work my way around this problem when manually writing the id in the url? 所以我的问题是:我如何告诉我的应用程序运行我的前端路由而不是后端路由,或者当在URL中手动写入ID时解决此问题的方法?

Any help would be greatly appreciated. 任何帮助将不胜感激。

I realise this isnt strictly answering your question, but unless you have a specific requirement to handle them on similar routes, isnt it simpler to serve your API on a clearly distinct route using a subdomain or specific base url fragment? 我意识到这并不是严格回答您的问题,但是除非您有在类似路径上处理它们的特殊要求,否则使用子域或特定基本url片段在明显不同的路径上提供您的API是否更简单?

Eg front end routes go on: www.example.com 例如,前端路线继续: www.example.com

API routes go on: www.api.example.com or www.example.com/api/ API路由继续: www.api.example.comwww.example.com/api/

Note that if you cant use the subdomain option, you'll need to configure URL rewrites in your web.config to route between your API and front end. 请注意,如果您不能使用subdomain选项,则需要在web.config配置URL重写以在API和前端之间路由。

Have you tried configuring your startup.cs? 您是否尝试过配置startup.cs? On the configure method try: 在configure方法上,尝试:

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

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

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

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