简体   繁体   English

使用Angular时,无法获取aspnet.core服务器路由以默认路径加载

[英]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.

相关问题 如何使用[HttpGetAttribute]路由包括AspNet.Core WebApi应用程序中的查询字符串? - How to route using [HttpGetAttribute] including the query strings in AspNet.Core WebApi application? 如何在 AspNet.Core 中读取 EXIF 数据 - How can i read EXIF data in AspNet.Core 在As.net.Core 中,我们可以更改端口号吗? - In Aspnet.Core, can we change port number? aspnet.core 使用 UserManager 播种数据<identityuser></identityuser> - aspnet.core Seeding data using UserManager<IdentityUser> 用于集成测试的ASPNet.Core HostingEnvironment? - ASPNet.Core HostingEnvironment for Integration Tests? 如何将.NetFramework 中的MessageHandler 转换为AspNet.Core - How to convert MessageHandler in .NetFramework to AspNet.Core 为什么 Automapper 在映射列表时返回 null<t> 在 aspnet.core 3.1 中?</t> - Why Automapper returns null while mapping List<T> in aspnet.core 3.1? AspNet.Core、IdentityServer 4:在使用 JWT 不记名令牌与 SignalR 1.0 进行 websocket 握手期间未经授权 (401) - AspNet.Core, IdentityServer 4: Unauthorized (401) during websocket handshake with SignalR 1.0 using JWT bearer token Aspnet.core 程序登录但考虑同一个表中的数据 - The Aspnet.core program logs in but considers the data in the same table 是否可以为ASPNET.Core OData声明多个路由 - Is it possible to declare multiple routes for ASPNET.Core OData
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM