【发布时间】:2014-08-15 01:31:24
【问题描述】:
是否可以让 MVC 应用程序仅使用来自 WEB API 的路由,然后允许 Angular 使用它的 routeprovider 来完成其余的路由?当我运行我的应用程序时,我得到:
GET http://localhost:8080/Views/Home/Index.html 404 (Not Found)
MVC 路由RouteConfig.Cs
// serves plain html
routes.MapRoute(
name: "DefaultViews",
url: "view/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
// Home Index page have ng-app
routes.MapRoute(
name: "AngularApp",
url: "{*.}",
defaults: new { controller = "Home", action = "Index" }
);
WebAPIConfig.cs
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
C# 控制器(尝试多种事情)
public ActionResult Index()
{
ViewBag.Title = "Home Page";
return View();
}
public ActionResult Test()
{
var result = new FilePathResult("~/Views/Home/test.html", "text/html");
return result;
}
public ActionResult Show()
{
ViewBag.Title = "HomePage";
return View();
}
角度路由:
app.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/Tests", { templateUrl: "Views/Home/test.html", controller: "homeCtrl" })
.when("/Shows", { templateUrl: "/Home/Show.cshtml", controller: "homeCtrl" })
.otherwise({ redirectTo: "/" });
});
文件结构图供参考:
编辑: GitHub 工作示例: https://github.com/allencoded/CSharpAngularRouting
【问题讨论】:
标签: c# asp.net-mvc angularjs