【问题标题】:ASP.NET MVC URL Routing with Single Route map带有单一路由映射的 ASP.NET MVC URL 路由
【发布时间】:2015-12-26 04:56:17
【问题描述】:

我正在尝试向我的 Asp.net MVC 网站添加正确的路由。

  • www.example.com/profile/39 ( Profile/{id} ):用于显示某人的个人资料。
  • www.example.com/profile/edit (Profile/edit):用于编辑当前用户配置文件

这是我的路线:

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


        routes.MapRoute("ShowProfile",
          "Profile/{id}",
      new { controller = "Profile", action = "Index" });


         routes.MapRoute(
             name:"Profile",
             url: "{controller}/{action}"
             );



        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

我的问题是,当我将 ShowProfile 路由放在首位时,它会正确显示配置文件,但是当我输入 www.example.com/profile/edit 时,它会显示缺少 Int 值的错误,而当将 Profile 路由放在首位时,会为 @ 显示 404 错误987654326@.
我试图通过改变我的路线来解决这个问题:

    public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("EditProfile",
            "Profile/Edit",
            new { controller = "Profile", action = "Edit" });

            routes.MapRoute("ShowProfile",
      "Profile/{id}",
  new { controller = "Profile", action = "Index" });

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }  

在这种格式下,这两种情况都可以正常工作,但对于其他发布方法,它会显示 404 错误,例如:

[HttpPost]
    [ValidateAntiForgeryToken]
       public ActionResult EditEmail()
    {

            return RedirectToAction("Index","Home");

    }

但我不想为我的所有方法添加路由值,有什么通用的方法可以不添加一个路由值吗?

【问题讨论】:

    标签: jquery asp.net ajax asp.net-mvc routes


    【解决方案1】:

    您需要检查您的控制器。可能它们确实不包含这些返回 HTTP404 的操作。

    这种方法对我很有效,所有 URL 都有效:

    • localhost:24934/profile/5
    • localhost:24934/profile/edit
    • 本地主机:24934/家
    • localhost:24934/home/main
    • localhost:24934/home/main/7

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult Main()
        {
            return View();
        }
    }    
    public class ProfileController : Controller
    {
        public ActionResult Edit()
        {
            return View();
        }
    
        public ActionResult Show(int id)
        {
            @ViewBag.id = id;
            return View();
        }
    }
    

    路线:

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute("EditProfile",
                "Profile/Edit",
                new {controller = "Profile", action = "Edit"});
    
            routes.MapRoute("ShowProfile",
                "Profile/{id}",
                new {controller = "Profile", action = "Show"});
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",    
                defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
                );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-05
      • 1970-01-01
      • 2011-11-11
      • 2012-09-03
      • 2011-04-10
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      相关资源
      最近更新 更多