AngularJS与ASP.NET MVC混淆 [英] AngularJS With ASP.NET MVC-Confused

查看:115
本文介绍了AngularJS与ASP.NET MVC混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:我已经在Stack Overflow中阅读了类似的问题,但是没有从这些答案中得到我想要的明确概念.

Please Note: I have read some questions like this in Stack Overflow but did't get the clear concept that I wanted from those answers.

对于为什么以及如何将AngularJS与ASP.NET Web API结合使用,我有明确的概念.但是我对将AngularJS与ASP.NET MVC结合使用感到困惑!

I have crystal clear concept of why and how to use AngularJS with ASP.NET Web API. But I am confused about the use of AngularJS with ASP.NET MVC!

对于具有ASP.NET Web API的AngularJS:

Web API控制器方法返回数据,AngularJS调用Web API控制器方法并捕获数据,然后在View中呈现数据.这非常合乎逻辑!

Web API controller method returns data and AngularJS call the Web API controller method and capture the data and then render the data in the View. This is pretty much logical!

如果是带ASP.NET MVC的AngularJS:

ASP.NET MVC控制器方法本身返回带有数据的View/View.那么将AngularJS与ASP.NET MVC一起使用是什么?

ASP.NET MVC Controller method itself returning View/View with data. Then what is the use of AngularJS with ASP.NET MVC?

尽管如此,如果我想将AngularJS与ASP.NET MVC一起使用,那么我必须从ASP.NET MVC控制器方法中返回JSON而不是MVC视图.

In spite of that, if I want to use AngularJS with ASP.NET MVC then I have to return JSON instead of MVC view from ASP.NET MVC controller method.

我的问题是:

  1. 出于将AngularJS与ASP.NET MVC结合使用的目的,从ASP.NET MVC控制器方法返回JSON而不是MVC视图是否合乎逻辑?如果这样做,有什么好处吗?
  2. 将AngularJS与ASP.NET MVC实际结合使用是什么?或在哪里可以将AngularJS与ASP.NET MVC一起使用?

推荐答案

从ASP.NET MVC返回JSON而不是MVC视图是否合乎逻辑 结合使用AngularJS和ASP.NET的控制器方法 MVC?如果这样做,有什么好处吗?

Is it logical to return JSON instead of MVC view from ASP.NET MVC controller method for the purpose of using AngularJS with ASP.NET MVC? If I do so, is there any benefit?

我想你可以.但是,如果只想返回数据,ASP.NET Web API确实是解决之道.好处会在您下一个问题中得到回答...

I would say that you can. But ASP.NET Web API is really the way to go if you only want to return data. And the benefits are answered in your next question...

将AngularJS与ASP.NET MVC实际结合使用是什么?或在哪里可以使用 带有ASP.NET MVC的AngularJS?

What is the actual use AngularJS with ASP.NET MVC? or Where can I use AngularJS with ASP.NET MVC?

首先,您使用的是什么而不是MVC?其他一些服务器端渲染?在这个答案中,我假设您获得了简单的html文件. MVC仍然可以与Angular一起使用.这有两种情况:

First, what are you using instead of MVC? Some other server-side-rendering? In this answer I assume that you got plain html-files. MVC can still be used with Angular. Here's a couple of scenarios:

1..假设您要限制对特定页面/路线的访问.在MVC中非常简单.

1. Let's say that you want to limit the access to a specific page/route. It's very simple in MVC.

public class HomeController : Controller
{
    [Authorize] // Authorize the user.
    public ActionResult Index()
    {
        return View();
    }
}

如果要使用Angular和Web API进行此操作,则将为用户提供HTML的访问权限,但不能访问数据.有点不同.使用MVC,您可以直接返回401.

If you want to do this with Angular and Web API you will give the user access to the HTML, but not the data. It's a bit different. With MVC you can return a 401 directly.

2..您可以省略显示HTML.

2. You can omit displaying HTML.

比方说,您获得了一些仅对某些用户可用的HTML.如果使用Angular隐藏/显示HTML,则它仍可用于客户端.但是,如果您使用MVC并且完全不呈现HTML,那么它将对用户不可用.

Let's say that you got some HTML that only should be available for some users. If you use Angular to hide/show the HTML it will still be available for the client. But if you use MVC and don't render the HTML at all it will not be available for the user.

角度:

<button type="submit" ng-hide="true" />

MVC:

@if(someCondition)
{
    <button type="submit" ng-hide="true" />
}

在客户端上隐藏一些东西,根本不向客户端提供信息有很大的区别.

There's a big difference in hiding something on the client, and not giving the information to the client at all.

我将MVC与Angular一起使用.我使用MVC渲染视图.然后,我让Angular从Web API中获取一些数据,然后显示特定于数据的内容.

I use MVC together with Angular. I use MVC to render the views. Then I let Angular fetch some data from Web API and then display the data-specific content.

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

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