如何进行不同的开发和生产路线

时间:2019-04-22 13:57:04

标签: c# asp.net-mvc

我需要在dev env和prod env之间建立一条不同的路线。

对于开发人员,我希望使用http://localhost:5000 但是我希望使用http://example.com/mdc

我在启动时尝试了以下

app.UseMvc(routes =>
        {
            if (env.IsDevelopment())
            {
                routes.MapRoute(
                       name: "login_dev",
                       template: "{controller=Account}/{action=Login}/{id?}",
                       defaults: new { controller = "Account", action = "Login" });
            }
            else
            {
                routes.MapRoute(
                 name: "login_prod",
                 template: "mdc/{controller=Account}/{action=Login}/{id?}",
                 defaults: new { controller = "Account", action = "Login" });
            }

            routes.MapRoute(
                name: "default",
                template: "{api}/{controller}/{action}");
        });

我知道了

Application startup exception
Microsoft.AspNetCore.Routing.RouteCreationException: An error occurred while creating the route with name 'login_prod' and template 'mdc/{controller=Account}/{action=Login}/{id?}'. ---> System.InvalidOperationException: The route parameter 'controller' has both an inline default value and an explicit default value specified. A route parameter cannot contain an inline default value when a default value is specified explicitly

1 个答案:

答案 0 :(得分:1)

您不能同时拥有inline defaultexplicit default。因此只需将您的路线更改为以下选项之一即可:

routes.MapRoute(
                 name: "login_prod",
                 template: "mdc/{controller=Account}/{action=Login}/{id?}");  

routes.MapRoute(
                 name: "login_prod",
                 template: "mdc/{controller}/{action}/{id?}",
                 defaults: new { controller = "Account", action = "Login" });