我试图使用Angulars $routeProvider将模板加载到我的Views文件夹中的.cshtml文件中的容器中.
.when('#/second',{ urlTemplate:"Views/second.cshtml",controller:"MainController"})@H_301_4@
为了向客户端提供.cshtml,您需要配置几个步骤.
我创建了一个名为AngularAspNet at GitHub的示例项目.
1)创建App文件夹以托管Angular Scripts.你可以说出你想要的任何名字.
2)将.cshtml文件放在App / xxx / Components /文件夹中.确保将属性设置为内容.
我想将Angular View和Component(JavaScript文件)放在一起.原因是当项目变得更加复杂时,它可以帮助我轻松找到View和相应的Component.因此,不是将.js放在Views文件夹中,而是将.chtml文件放在App(Angular Script)文件夹中.
1)在里面创建一个带有BlockViewHandler的web.config.
<?xml version="1.0"?> <configuration> <configSections> ... <system.webServer> <handlers> <remove name="BlockViewHandler"/> <add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> </handlers> </system.webServer> ... </configuration>@H_301_4@4)创建控制器和操作方法.
public class NgController : Controller { public PartialViewResult RenderComponents(string feature,string name) { return PartialView($"~/App/{feature}/Components/{name}"); } }@H_301_4@5)配置路由.
routes.MapRoute( name: "Components",url: "{feature}/components/{name}",defaults: new { controller = "Ng",action = "RenderComponents" });@H_301_4@信用
Building Strongly-typed AngularJS Apps with ASP.NET MVC 5
by Matt HoneycuttAngularJS & ASP.NET MVC Playing nice Tutorial by Miguel Castro