如何在mvc4中创建自定义路由

时间:2018-08-04 13:05:34

标签: c# asp.net-mvc-4 routes

我想在RedirectToAction之后喜欢这个(/ User / Index?abc)网址
怎么做请给我建议。

RouteConfig.cs

routes.MapRoute(
   name: "Putabc",
   url: "{tempUrl}/{RegNo}",
   defaults: new { controller = "User", action = "Index", RegNo = UrlParameter.Optional}
);

控制器:-

public ActionResult Putabc() 
{ 
    string RegNo = "abc"; 
     string tempUrl = Url.Action("Index", "User");
    return RedirectToAction(new { url = tempUrl + "?{RegNo}" }, JsonRequestBehavior.AllowGet);
}  

1 个答案:

答案 0 :(得分:0)

请尝试这个

内部RouteConfig

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

//注意:“ Home”和“ Index”是您的默认控制器和操作

控制器

 [RoutePrefix("User")]

public class SomeController : Controller
{  
 //This action will be hit by 

 // www.yoursite.com/User/Index?url=some
   public ActionResult Index(string url) 
   { 

   }  
}

HomeController

public class HomeController : Controller
{ 
   public ActionResult Putabc() 
  { 
     string RegNo = "abc"; 
     string tempUrl = Url.Action("Index", "User");
      return RedirectToAction(new { url = tempUrl + "?{RegNo}" }, 
     JsonRequestBehavior.AllowGet);
  }  
}