与MVC 6 AngularJS [英] AngularJS with MVC 6

查看:143
本文介绍了与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.

该项目页面: https://github.com/a11smiles/AngularMiddleware

这篇关于与MVC 6 AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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