MVC重定向-自定義路由篇

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

            routes.MapRoute(
                 name: "Rewrite",
                 url: "product/c{Industry}/{id}",
                 defaults: new { controller = "Rewrite", action = "NIndex", id = UrlParameter.Optional },constraints:new {id=@"\d+", Industry=@"\d+"}

             );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
 public class RewriteController : Controller
    {
        // GET: Rewrite
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult NIndex(int Industry, int id)
        {
            ViewBag.Result = string.Format("該產品的行業是{0},編號是{1}",Industry,id);
            return View("Index");

        }
    }
相關文章
相關標籤/搜索