jats
jats

Reputation: 137

URl rewriting in mvc

Hy

i had write below code

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

i am new to mvc,i want to rewrite my url somthing like this,suppose my url is like www.myhouse.com/product/index/1 then i want to display only www.myhouse.com/prduct-name for better seo performance,i am using mvc4 beta,i had also one through URL Rewriting in .Net MVC but it is not working for me....

but i don't know how to pass pass value to this method.

Upvotes: 0

Views: 7215

Answers (1)

jats
jats

Reputation: 137

After lots of searching on the internet, i got my solution

add below code to 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
         );

then add below code to view:

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

add below code to HomeController:

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

then you done...now URL is same as you pass in query string...so your controller name and query string parameter will not display.

Upvotes: 2

Related Questions