[英]Can't get aspnet.core server route to load with default path, when using Angular
I want all calls that are not caught by any server side routes to load my spa. 我希望所有未被服务器端路由捕获的呼叫都加载我的spa。 So in my
startup.cs
file I have the following 所以在我的
startup.cs
文件中,我有以下内容
// routes
app.UseMvc(routes =>
{
// default goes to Home, and angular will deal with client side routing
routes.MapRoute(
name: "default",
template: "{*url}",
defaults: new { controller = "home", action = "index" });
});
I then have a server page that I want to load without adding the word "index" to the path, so it should load the index view when I go to http://localhost:5000/serverRoute
but it always tries to load my spa. 然后,我有一个要加载的服务器页面,而未在路径中添加单词“ index”,因此当我转到
http://localhost:5000/serverRoute
时,它应该加载索引视图,但是它总是尝试加载我的spa 。 If however I change the URL to http://localhost:5000/serverRoute/index
then the server page loads. 但是,如果我将URL更改为
http://localhost:5000/serverRoute/index
则会加载服务器页面。
My ASP.net core controller looks like so 我的ASP.net核心控制器看起来像这样
[Route("[controller]/[action]")]
[ApiExplorerSettings(IgnoreApi = true)]
public class ServerRouteController : Controller
{
/// <summary>
/// Load default index view
/// </summary>
/// <returns></returns>
public ViewResult Index()
{
return View();
}
}
What am I doing something wrong? 我做错了什么?
The controller action token in the route template is not optional, which is why is does not match when not present in the URL. 路由模板中的控制器操作令牌不是可选的,这就是为什么URL中不存在时控制器令牌不匹配的原因。
[Route("[controller]")]
[ApiExplorerSettings(IgnoreApi = true)]
public class ServerRouteController : Controller {
/// <summary>
/// Load default index view
/// </summary>
/// <returns></returns>
[Route("")] //Match /ServerRoute
[Route("[action]")] //Match /ServerRoute/Index
public IActionResult Index() {
return View();
}
}
Reference Routing to Controller Actions in ASP.NET Core 参考路由到ASP.NET Core中的控制器操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.