您需要配置几个步骤才能将 .cshtml 提供给客户端。
我创建了一个名为AngularAspNet at GitHub 的示例项目。
1) 创建 App 文件夹来托管 Angular 脚本。 你可以随意命名。
2) 将 .cshtml 文件放在 App/xxx/Components/ 文件夹中。确保将属性设置为内容。
我想将 Angular 视图和组件(JavaScript 文件)放在一起。原因是当项目变得更复杂时,它可以帮助我轻松找到视图和相应的组件。因此,我没有将 .js 放在 Views 文件夹中,而是将 .chtml 文件放在 App(Angular Script)文件夹中。
1) 创建一个 web.config,其中包含 BlockViewHandler。
<?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>
4) 创建控制器和动作方法。
public class NgController : Controller
{
public PartialViewResult RenderComponents(string feature, string name)
{
return PartialView($"~/App/{feature}/Components/{name}");
}
}
5) 配置路由。
routes.MapRoute(
name: "Components",
url: "{feature}/components/{name}",
defaults: new { controller = "Ng", action = "RenderComponents" });
信用
Building Strongly-typed AngularJS Apps with ASP.NET MVC 5
by Matt Honeycutt
AngularJS & ASP.NET MVC Playing nice Tutorial by Miguel Castro