0

I have a route configured like

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

It works fine to redirect to respective controller and actions.

I want to add another redirection on TEST so that if somebody uses www.mysite.com/TEST, it should redirect www.mysite.com/Test/Home instead of giving 403- Forbidden: Access is denied error.

I'm trying like this but could not achieve it.

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

Basically, what I'm trying to do is to redirect from www.mysite.com or www.mysite.com/TEST to www.mysite.com/TEST/Home

To add to the confusion, I also had a physical folder TEST in my application root. Just wondering if keeping another web.config in there would solve? I tried but of no luck

Please advise what i'm missing here. Thanks

After some experiment I have found that the physical folder TEST is causing redirection rule to fail. I changed my route to TEST1 in URL instead of TEST, it worked. But, I can't rename TEST folder. Please advise

9
  • Have a look at the order in which these routes are added. Commented Nov 20, 2015 at 16:09
  • yes, i tried that too Commented Nov 20, 2015 at 16:10
  • I'm not sure but it may be because of physical folder TEST, I get 403 - Forbidden error Commented Nov 20, 2015 at 16:15
  • Perhaps remove the redundant default id from "AnotherDefault"? Do you have "RouteConfig.RegisterRoutes(RouteTable.Routes);" in your Application_Start() in the Global.asax? Commented Nov 20, 2015 at 16:15
  • yes, I have it already in Global.asax; my routing works if I use www.mysite.com/TEST/mycontroller/myaction; but my only problem is i'm not able to redirect www.mysite.com/TEST to www.mysite.com/TEST/Home. I have a physical folder also called TEST. I'm only worried if it is conflicting with my routing Commented Nov 20, 2015 at 16:17

3 Answers 3

2

Please set the property RouteExistingFiles to true above the Route configurations

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

    }

This should allow you to keep the name of folder and also the route name to be "TEST". Let me know how it works out for you

Sign up to request clarification or add additional context in comments.

2 Comments

awesome!! this is what i'm looking for. thanks jonni
it created another problem... Can you please see stackoverflow.com/questions/33832209/…?
0

Keep using the first route:

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

and add this in your Web.config:

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

1 Comment

nope, still the same issue. Please see my updated question
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;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.