Angular 路由模板 url 是否支持 ASP.Net MVC 5 项目中的 *.cshtml 文件? [英] Does Angular routing template url support *.cshtml files in ASP.Net MVC 5 Project?
问题描述
我正在处理一个 MVC 5 项目.当我在视图中使用 html 页面时,它会加载该页面,但是当我使用 .cshtml 页面时,它不会加载视图.出现空白页.
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'
})
请指导我如何使用cshtml文件或最好的方法.
Please guide me how to use cshtml file or the best way to do it.
推荐答案
是的,你可以.
向 Yasser 的添加类似的答案,但使用 ngRoute:
Yes, you can.
Adding a similar answer to Yasser's, but using ngRoute:
1) 您需要将控制器/操作引用到您的 ASP.NET MVC 应用程序,而不是引用您的部分 HTML.
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) 您的 ASP.NET MVC 将返回一个 .cshtml 视图:
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) 您的 MyView.cshtml 将混合 Razor 和 Angular.注意:作为您的 Angular 应用程序的一部分,请将布局设置为 null.
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 -->
这篇关于Angular 路由模板 url 是否支持 ASP.Net MVC 5 项目中的 *.cshtml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!