使用单路由映射的ASP.NET MVC URL路由

时间:2015-09-28 20:53:55

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

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

  • www.example.com/profile/39(Profile / {id}):用于显示某个人的个人资料。
  • www.example.com/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路由时首先显示www.example.com/profile/39的404错误 我试图通过改变我的路线来解决这个问题:

    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");

    }

但我不想为我的所有方法添加路由值,是否有任何一般方法可以不逐个添加路由值?

1 个答案:

答案 0 :(得分:0)

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

这种方法适用于我,所有网址都有效:

  • localhost:24934 / profile / 5
  • 本地主机:24934 /简档/编辑
  • localhost:24934 / home
  • 本地主机:24934 /家/主
  • 本地主机:24934 /家/主/ 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}
            );
    }
}