Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Angular routing template url support *.cshtml files in ASP.Net MVC 5 Project?

Tags:

I am working on a MVC 5 project. When I use a html page at my views, it load that page but when I use .cshtml page it is not loading the view. The Blank page appears.

$urlRouterProvider     .otherwise('/app/dashboard');  $stateProvider                 .state('app', {         abstract: true,         url: '/app',         templateUrl: 'tpl/app.html'     })     .state('app.dashboard', {         url: '/dashboard',         templateUrl: 'tpl/app_dashboard.html'     }) 

Please guide me how to use cshtml file or the best way to do it.

like image 921
Faizan Avatar asked Sep 11 '14 10:09

Faizan


People also ask

Can we use angular with ASP.NET MVC?

To load Angular in ASP.NET MVC, include the script references of Angular core modules and Syncfusion JavaScript asset files in _Layout file , and load the component in index. cshtml like the following code snippets.

Why use angular instead of MVC?

Angular does away with the problem of mixing client and server code within the same file. Razor syntax allows the developer to embed client and server code within the same file and to use server-side logic to control the client-side presentation. Angular decouples the client-side processing completely from the server.

Is MVC better than angular?

Both ASP.NET MVC and AngularJS has their own purposes and advantages. As with your specific question, AngularJs is better for SPA (Single Page Applications), where as ASP.NET MVC is a full fledged server side Application which can contain WebAPIs, Routing engine as well as HTML emitting Views.


2 Answers

Yes, you can.

Adding a similar answer to Yasser's, but using ngRoute:

1) Instead of referencing your partial HTML, you need to reference a Controller/Action to your ASP.NET MVC app.

.when('/order', {     templateUrl: '/Order/Create',     controller: 'ngOrderController' // Angular Controller }) 

2) Your ASP.NET MVC will return a .cshtml view:

public class OrderController : Controller {     public ActionResult Create()     {         var model = new MyModel();         model.Test= "xyz";          return View("MyView", model);     } } 

3) Your MyView.cshtml will mix Razor and Angular. Attention: as its a partial for your Angular app, set the layout as null.

@model MyProject.MyModel  @{    Layout = null; }  <h1>{{ Test }}</h1> <!-- Angular --> <h1>Model.Test</h1> <!-- Razor --> 
like image 79
Zanon Avatar answered Sep 25 '22 23:09

Zanon


You cannot.

You cannot use path to a .cshtml file in your template url of your angular route. Can you open the .cshtml file in your browser ? No right ? The same thing applies here.

Workaround :

.cshtml file contains server side code, to render it you need to use a controller and action. They will render that .cshtml file for you. So have a controller action return that view for you and use the url to the controller action in your angular route.

eg:

state('app.dashboard', {         url: '/dashboard',         templateUrl: '/dashboard/index'      }) 

with

public class DashboardController : Controller {     public ActionResult Index()     {         return View();     } } 

p.s: have tried and this worked for me

like image 42
Yasser Shaikh Avatar answered Sep 24 '22 23:09

Yasser Shaikh