且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular路由模板URL是否支持ASP.Net MVC 5项目中的* .cshtml文件?

更新时间:2022-12-14 23:25:07

是的,你可以。



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