TanvirArjel TanvirArjel - 14 days ago 7
C# Question

AngularJS With ASP.NET MVC-Confused

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. So please don't mark as duplicate!!

...........................................................................

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!!

In Case of AngularJS with ASP.NET Web API:

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!

In Case of AngularJS with 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??

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.

My Questions are:


  1. 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??

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



Expceting answers from experts!!Thanks!!

Answer

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??

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...

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

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. 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();
    }
}

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. You can omit displaying 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.

Angular:

<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.

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.