简体   繁体   English

如何同时使用AngularJS作为前端框架和ASP.NET MVC作为后端框架?

[英]how to use AngularJS as front-end framework and ASP.NET MVC as back-end framework together?

I have architected a project as follows: 我设计的项目如下:

asp.net mvc as back-end framework asp.net mvc作为后端框架

angularjs as front-end framework angularjs作为前端框架

But I think the project is not readable and standard for example asp.net mvc has routing for managing url(s) and angularjs has another technique at client side (it use # at the end of url(s)). 但是我认为该项目不可读且不标准 ,例如asp.net mvc具有用于管理url的路由 ,而angularjs在客户端具有另一种技术(它在url的末尾使用#)。

Is it usual to use angularjs and asp.net mvc together? 通常一起使用angularjs和asp.net mvc吗?

Is there any restriction for choosing a back-end technology like Node.js with angularjs? 选择带有angularjs的Node.js等后端技术有任何限制吗?

Could you send me a sample code that shows asp.net mvc and angularjs working together? 您能给我发送一个示例代码来显示asp.net mvc和angularjs一起工作吗?

I know asp.net mvc has lots of feature for client side (html) like html.helpers and so on and i know angularjs has lots of feature on client side (html) like expressions ({{Property}}) , binding (ng-bind) and so on. 我知道asp.net mvc在客户端(html)上有很多功能,例如html.helpers等,而且我知道angularjs在客户端(html)上有很多功能,例如表达式({{Property}})绑定(ng -bind)等等。

I'm confused. 我糊涂了。

How to match features of two frameworks together. 如何将两个框架的特征匹配在一起。

Our last web project use Angular on the client and MVC on the backend. 我们的最后一个Web项目在客户端使用Angular,在后端使用MVC。

We used MVC for routing but used the location server for web params. 我们使用MVC进行路由,但使用位置服务器进行Web参数。 So urls looked like 所以网址看起来像

http://site/Customer/Edit/#12345 (12345 being the id) http://site/Customer/List (Returns a list of Customers) http:// site / Customer / Edit /#12345 (ID为12345) http:// site / Customer / List (返回客户列表)

etc 等等

In the controller we have code like this to populate data 在控制器中,我们有像这样的代码来填充数据

innerHttp = $http.get("Data" + $location.path());
innerHttp.success(function (innerResponse) {
    console.log("Form Data Loaded");
    $scope.entity = innerResponse.data || {};
}).error(function (data, status, headers, config) {
    $scope.addAlert('danger', 'Error loading form data (' + status.toString() + ')');
});

Notice how we get the ID from the location service. 注意我们如何从定位服务获取ID。

Our control has a View to return the HTML form and a View that returns the Data, this one looks like 我们的控件有一个View返回HTML表单和一个View返回Data,这看起来像

[Route("{type}/data/{id}")]
public ActionResult Data(string type, Guid id)
{
    return new JsonNetResult()
    {
        Formatting = Formatting.Indented,
        Data = DataBuilder.ReadData(type,id) 
    };
}

This approach does not give you a single page app, but it still separates pages from data and allows the client to cache the html, only json calls will need to be fresh so keeps bandwidth down and gets you into angular at the same time. 这种方法不会为您提供单个页面的应用程序,但是它仍然可以将页面与数据分开,并允许客户端缓存html,只有json调用才需要刷新,因此可以降低带宽并同时​​使您陷入困境。

Hope this helps you. 希望这对您有所帮助。

Ideally you should use ASP.Net WebAPI for a rest service backend. 理想情况下,您应该将ASP.Net WebAPI用于其余服务后端。 For the frontend UI you would use Angularjs by itself with no dependencies on ASP.Net other than the WebAPI JSON over Rest service. 对于前端UI,您可以单独使用Angularjs,除了WebAPI JSON over Rest服务之外,不依赖ASP.Net。

You would not end up using a lot that ASP.Net MVC has to offer, but you would be left with a clear separation of the backend service layer and the frontend UI. 您最终不会使用ASP.Net MVC所提供的大量功能,但是您将剩下后端服务层和前端UI的清晰分隔。

This will remove the possibility of needing to duplicate functionality in both ASP.Net MVC and AngularJS. 这将消除在ASP.Net MVC和AngularJS中都需要重复功能的可能性。

You're right that fundamentally ASP.NET MVC and AngularJS provide a lot of the same services, but executed at different layers of the stack. 没错,从根本上讲,ASP.NET MVC和AngularJS提供了很多相同的服务,但是在堆栈的不同层执行。 If you're building a single-page web application that is heavy on the client, then you won't end up using much of ASP.NET MVC's functionality except as perhaps an intermediate layer to get to your data. 如果您要构建的单页面Web应用程序在客户端上很繁琐,那么您最终将不会使用ASP.NET MVC的许多功能,除非将其用作获取数据的中间层。 If you prefer more of a server-centered approach where you're actually navigating to different server URLs on each page, then you won't have as much Angular in your app. 如果您更喜欢以服务器为中心的方法,而您实际上要在每个页面上导航到不同的服务器URL,那么您的应用程序中将没有太多的Angular。

Still, I too am building a new web app right now that uses both ASP.NET MVC and AngularJS, and mine is a single-page web app that is heavy on the client-side code. 尽管如此,我现在也正在构建一个同时使用ASP.NET MVC和AngularJS的新Web应用程序,而我的是一个单页面Web应用程序,该应用程序非常重视客户端代码。 I don't think there's anything wrong with this approach, but in a sense I suppose it could be argued that ASP.NET MVC is a bit overkill for this design. 我认为这种方法没有什么问题,但是从某种意义上说,我认为可以辩驳说ASP.NET MVC对于这种设计有些过分了。 Perhaps that is one of the reasons people gravitate toward Node.js. 也许这就是人们偏向Node.js的原因之一。

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

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