Angular路由模板URL是否支持ASP.Net MVC 5项目中的* .cshtml文件? [英] Does Angular routing template url support *.cshtml files in ASP.Net MVC 5 Project?

查看:277
本文介绍了Angular路由模板URL是否支持ASP.Net MVC 5项目中的* .cshtml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个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.

推荐答案

是的,你可以。



为<添加类似的答案a href =https://stackoverflow.com/a/27182836/1476885> Yasser's ,但使用ngRoute:

Yes, you can.

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

1)而不是引用你的部分HTML,您需要将Controller / Action引用到您的ASP.NET MVC应用程序。

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

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