mardi 7 juillet 2015

MVC routing without controller name in URL

I am new to MVC and trying to make a blog engine just to learn new things about mvc, so I want my urls like these

1- example.com //Home page

2- example.com/post-url //Post page

My default router is like this

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

which works perfect for home page and routing to Index action in the Home controller.

But post url work like this /Home/Index/post-query and I have one Action in the Home controller, Which is Index, and I am passing optional query string to that Action.

but I want url like this /post-query

EDIT: Home Controller

This is just a example controller, I don't have a Post View present at the moment.

public ActionResult Index(string query)
        {
            if (query != null)
            {
                return View("Post");
            }
            else
            {
                return View("Index");
            }
        }

Aucun commentaire:

Enregistrer un commentaire