C# 由于我为控制器的特定操作编写了路由,我是否需要为控制器内的所有操作编写路由?

C# 由于我为控制器的特定操作编写了路由,我是否需要为控制器内的所有操作编写路由?,c#,asp.net-mvc,routing,asp.net-mvc-routing,maproute,C#,Asp.net Mvc,Routing,Asp.net Mvc Routing,Maproute,我正在为我的MVC应用程序编写一些路由。我有以下申请途径: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional } ); 当我

我正在为我的MVC应用程序编写一些路由。我有以下申请途径:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
            );
当我想要访问以下默认值时,使用上述路由:

www.servicili.com/budget/edit/1

www.servicili.com/professional/view/234

但是,我创建以下路线是出于特定目的:

routes.MapRoute(
                name: "Perfil",
                url: "{UsuApelido}",
                defaults: new { controller = "Perfil", action = "Index"}
            );
上面的路由用于访问“水管工”的URL配置文件,例如: www.servicili.com/MarkZuckberg

配置文件详细信息位于控制器Perfil和操作索引上,但是,由于我编写了此路由,所有其他操作都不起作用

例如:如果我试图访问另一个控制器内的索引操作,它将重定向到Perfil的索引

--
问题是:由于我为控制器的特定操作编写了路由,我是否需要为控制器内的所有操作编写路由?

要解决您的问题,请尝试这样做

public ActionResult Index(string UsuApelido)
{
  //load the content from db with UsuApelido
  //display the content with view
}
首先定义约束条件

public class PlumberUrlConstraint: IRouteConstraint
{
   public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
   {
      var db = new YourDbContext();
      if (values[parameterName] != null)
      {
        var UsuApelido = values[parameterName].ToString();
        return db.Plumbers.Any(p => p.Name == UsuApelido);
      }
      return false;
   }
}
定义两条路线,将“默认”路线放在第二个位置

routes.MapRoute(
            name: "Perfil",
            url: "{*UsuApelido}",
            defaults: new { controller = "Perfil", action = "Index"},
            constraints: new { UsuApelido = new PlumberUrlConstraint() }
        );

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
        );
现在,如果您在“Perfil”控制器中有一个“Index”操作,您可以得到如下管道工名称:

public ActionResult Index(string UsuApelido)
{
  //load the content from db with UsuApelido
  //display the content with view
}

希望这有帮助。

要解决您的问题,请尝试这样做

public ActionResult Index(string UsuApelido)
{
  //load the content from db with UsuApelido
  //display the content with view
}
首先定义约束条件

public class PlumberUrlConstraint: IRouteConstraint
{
   public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
   {
      var db = new YourDbContext();
      if (values[parameterName] != null)
      {
        var UsuApelido = values[parameterName].ToString();
        return db.Plumbers.Any(p => p.Name == UsuApelido);
      }
      return false;
   }
}
定义两条路线,将“默认”路线放在第二个位置

routes.MapRoute(
            name: "Perfil",
            url: "{*UsuApelido}",
            defaults: new { controller = "Perfil", action = "Index"},
            constraints: new { UsuApelido = new PlumberUrlConstraint() }
        );

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
        );
现在,如果您在“Perfil”控制器中有一个“Index”操作,您可以得到如下管道工名称:

public ActionResult Index(string UsuApelido)
{
  //load the content from db with UsuApelido
  //display the content with view
}

希望对您有所帮助。

只需修复代码并在末尾添加}即可:新建。。。你知道如何在MVC中学习创建路由的链接吗?tks只需修复代码并在末尾添加}即可:新建。。。你知道如何在MVC中学习创建路由的链接吗?非常感谢