【问题标题】:URl rewriting in mvc在 mvc 中重写 URl
【发布时间】:2012-07-14 02:12:16
【问题描述】:

Hy

我写了下面的代码

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Home", // Route name
            "", // URL with parameters
            new { controller = "Home", action = "Index" } // Parameter defaults
        );
        routes.MapRoute(
                   "Controller_Action", // Route name
                   "{controller}/{action}/{id}", // URL with parameters
                   new { id = UrlParameter.Optional } // Parameter defaults
        );
        foreach (var route in GetDefaultRoutes())
        {
            routes.Add(route);
        }
        routes.MapRoute(
            "UserPage", // Route name
            "{id}", // URL with parameters
            new { controller = "Home", action = "Get" } // Parameter defaults
        );
    }




    private static IEnumerable<Route> GetDefaultRoutes()
    {
        //My controllers assembly (can be get also by name)
        Assembly assembly = typeof(test1.Controllers.HomeController).Assembly;
        // get all the controllers that are public and not abstract
        var types = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Controller)) && t.IsPublic && !t.IsAbstract);
        // run for each controller type
        foreach (var type in types)
        {

            //Get the controller name - each controller should end with the word Controller
            string controller = type.Name.Substring(0, type.Name.IndexOf("Controller"));
            // create the default
            RouteValueDictionary routeDictionary = new RouteValueDictionary
                                               {
                                                   {"controller", controller}, // the controller name
                                                   {"action", "index"} // the default method
                                               };
            yield return new Route(controller, routeDictionary, new MvcRouteHandler());
        }
    }

我是 mvc 的新手,我想像这样重写我的 url,假设我的 url 像 www.myhouse.com/product/index/1 那么我只想显示 www.myhouse.com/prduct-name为了获得更好的 seo 性能,我正在使用 mvc4 beta,我也有一个到URL Rewriting in .Net MVC,但它不适合我....

但我不知道如何将值传递给此方法。

【问题讨论】:

  • 为什么使用“mvc”(语言无关设计模式的名称)来描述 ASP.NET MVC 4 框架?你是不是也将桌面上的 IE 图标称为“互联网”的人之一?
  • 这不是将桌面上的IE图标称为“interent”,在mvc中这可能我不知道这就是我问你的原因......
  • 你可以使用url重写(属性重写)请查看我给的链接sreerejith.blogspot.in/2015/07/…

标签: asp.net-mvc asp.net-mvc-routing


【解决方案1】:

在网上查了很多资料,终于找到解决办法了

将以下代码添加到global.asax

routes.MapRoute(
            "Home", // Route name
            "", // URL with parameters
            new { controller = "Home", action = "Index" } // Parameter defaults
        );
        routes.MapRoute(
           "jats", // Route name
           "{jats}", // URL with parameters
           new { controller = "Home", action = "Content" } // Parameter defaults
         );

然后添加以下代码以查看:

@Html.ActionLink("test", "Content", new { jats= "test-test" })

将以下代码添加到HomeController:

public ActionResult Content(string jats)
{
    return View();
}

那么你就完成了...现在 URL 与您传入的查询字符串相同...因此您的控制器名称和查询字符串参数将不会显示。

【讨论】:

  • 参数呢?
猜你喜欢
  • 2015-07-15
  • 1970-01-01
  • 2011-04-23
  • 2016-03-28
  • 2011-02-07
  • 2012-06-03
  • 2014-09-26
  • 2018-10-11
  • 2012-08-06
相关资源
最近更新 更多