与MVC 6 AngularJS

编程入门 行业动态 更新时间:2024-10-21 05:43:41
本文介绍了与MVC 6 AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个 MVC 6 /的WebAPI 应用程序。我试图在后端使用的WebAPI与前端AngularJS。

This is an MVC 6/WebApi application. I'm attempting to use WebApi on the backend with AngularJS on the frontend.

我有两个静态文件:index.html,然后login.html的结果(有最终将更多的静态文件。)

I have two static files: index.html and login.html (There will eventually be more static files.)

我的角度应用都包含在index.html的,而视图(如/登录)从静态文件(即login.html的)。

My Angular app is contained in the index.html while views (such as /login) are loaded from static files (i.e. login.html).

如果我去我的根URL,index.html的加载就好了。但是,如果我去/登录,我收到404页未找到。

If I go to my root URL, index.html is loaded just fine. However, if I go to /login, I receive a 404 page not found.

现在,让它可以说,我的不要想与一群控制器和视图来拖垮我的应用程序,因为它是没有必要简单地提供静态文件。这是矫枉过正。

Now, let it be said, I DO NOT want to bog down my application with a bunch of controllers and views as it's unnecessary to simply serve static files. That's overkill.

我有我的路线设置服务API调用(即〜/ API /不管)。我如何获得MVC 6忽略所有其他路线?

I have my routes setup to serve API calls (i.e. ~/api/whatever). How I get MVC 6 to ignore all other routes?

仅供参考,它也出现了的的MapPageRoute 的是德$ P $在MVC 6 pcated?

FYI, it also appears that MapPageRoute is deprecated in MVC 6?

编辑:结果有一件事我已经做了真正得到它的工作是添加自己的中间件拦截请求。在code以下毕竟是我的默认中间件添加在配置的方法:

app.Use(next => async context => { if (context.Request.Path.Value.StartsWith("/api")) await next.Invoke(context); else await context.Response.WriteAsync(System.IO.File.ReadAllText("index.html")); });

这似乎有点多,它只是一个黑客。它所做的就是允许用/ API,通过走(然后由MVC的中间件拾起)开头的请求,但其他呼叫送达的内容的 index.html的的。所以,如果我的请求的URL是/登录,在的index.html的文件送达,的然后的角度着眼于路线和加载的的login.html 的文件进入视野。

This seems a bit much and it's just a hack. All it does is allows any request that begins with "/api" to go through (which is then picked up by the MVC middleware), but any other call is served with the contents of the index.html. So, if my requested URL is "/login", the index.html file is served, then Angular looks at the route and loads the login.html file into view.

任何其他建议?

推荐答案

好了,因为东西没有在MVC 6 / ASP.NET框架5的存在,我已经创建了自己的中间件,它提供了更大的灵活性。它已被添加到GitHub上,而且可通过的NuGet

Okay, so since something didn't exist in the MVC 6/ASP.NET 5 framework, I've created my own middleware that provides a lot more flexibility. It has been added to GitHub and is available through NuGet.

该项目页面: github/a11smiles/AngularMiddleware

更多推荐

与MVC 6 AngularJS

本文发布于:2023-11-08 13:56:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MVC   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!