MVC自定义网址路由

时间:2015-11-20 15:54:34

标签: asp.net-mvc url-routing

我的路线配置为

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

可以正常重定向到相应的控制器和操作。

我想在TEST上添加另一个重定向,这样如果有人使用www.mysite.com/TEST,它应该重定向www.mysite.com/Test/Home而不是给出403- Forbidden:拒绝访问错误。

我正在尝试这样但却无法实现它。

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

基本上,我要做的是从www.mysite.com或www.mysite.com/TEST重定向到www.mysite.com/TEST/Home

为了增加混乱,我的应用程序根目录中还有一个物理文件夹TEST。只是想知道在那里保留另一个web.config会解决吗?我试过但没有运气

请告知我在这里缺少什么。感谢

经过一些实验后,我发现物理文件夹TEST导致重定向规则失败。我在URL而不是TEST中将路由更改为TEST1,它有效。但是,我无法重命名TEST文件夹。请指教

3 个答案:

答案 0 :(得分:2)

请在路由配置

上方将属性RouteExistingFiles设置为true
public static void RegisterRoutes(RouteCollection routes)
    {
        routes.RouteExistingFiles = true;

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

    }

这应该允许您保留文件夹的名称以及路径名称为“TEST”。让我知道它是如何为你工作的

答案 1 :(得分:0)

继续使用第一条路线:

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

并在Web.config中添加:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"></modules>
  <handlers>
    <remove name="UrlRoutingHandler"/>
  </handlers>
</system.webServer>

答案 2 :(得分:0)

We Can manage url routing by validate from database.
    public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 // here i pass my parameter like  wwww.abc.com/country-state-city 

        routes.MapLocalizedRoute("SeoFriendlyUrl",
        "{SeoFriendlyName}",
        new { controller = "Company", action = "Index" },
        new[] { "MigrationTest.Controllers" });
      // it is default 
        routes.MapRoute( name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

}

public static class LocalizedRouteExtensionMethod
{
    public static Route MapLocalizedRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces)
    {
        return MapLocalizedRoute(routes, name, url, defaults, null /* constraints */, namespaces);
    }
    public static Route MapLocalizedRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces)
    {
        if (routes == null)
        {
            throw new ArgumentNullException("routes");
        }
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }

        var route = new clsRouteData(url, new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(defaults),
            Constraints = new RouteValueDictionary(constraints),
            DataTokens = new RouteValueDictionary()
        };

        if ((namespaces != null) && (namespaces.Length > 0))
        {
            route.DataTokens["Namespaces"] = namespaces;
        }

        routes.Add(name, route);

        return route;
    }
}
public class clsRouteData : Route
{
    public clsRouteData(string url, IRouteHandler routeHandler)
        : base(url, routeHandler)
    {
    }
    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        RouteData data = base.GetRouteData(httpContext);
        if (data != null)
        {
            var SeoFriendliyName = data.Values["SeoFriendlyName"] as string;

        if (SeoFriendliyName=="india-raj-jaipur")
            {
            data.Values["controller"] = "City";
            data.Values["action"] = "Index";
            // Can be send parameter
            //data.Values["Id"] = Resutls.Id;
            }

        }
  else
 {

data.Values["controller"] = "Norecord";
            data.Values["action"] = "Index";
            // Can be send parameter
            //data.Values["Id"] = Resutls.Id;
}
        return data;
    }

}