KMX
KMX

Reputation: 2681

MVC 5 how to work with custom URLs or Url Rewriting

I have a gallery website. That developed in asp.net. Now I want to port it to asp.net MVC5. While doing it I run into an issue without solving it I cant go into further development. So here is the requirements:

a) Every gallery has its own independent folder and has photos in it. the url for the photo galleries will be: localhost/gallery1, localhost/gallery2 and so on.

b) Each gallery has two types of layouts and options the commbo is given bellow:

localhost/gallery1/still/grid
localhost/gallery1/still/slide
localhost/gallery1/gifs/grid
localhost/gallery1/gifs/slide

c) change of [gallery1] to any other name should serve the photos from within the new name folder.

d) we also have a way to configure gallery for each folder by accessing admin

localhost/gallery1/admin
localhost/gallery2/admin

I am new to MVC. And I dont know how to work with that... I just created a brand new MVC5 project using Visual Studio with builtin example. Can anyone help me how to deal with this?

EDIT: The controller has to be a universal control for anything immediately after localhost/ so in my example urls gallery1 and gallery2 or anything like that will be caught by a universal controller, may be GalleryController.

After the name of the gallery, the next two url segments are going to work in conjugation. I am not sure how to catch them in a universal controller and then segment them out on the basis of what is after still and gifs.

Upvotes: 1

Views: 737

Answers (1)

KMX
KMX

Reputation: 2681

I have found answer to my own question. Here is what i did.

Inside RouteConfig.cs I simply added following mapping:-

routes.MapRoute(
                name: "Dynamic",
                url: "{name}/{action}/{layout}",
                defaults: 
                new { 
                       controller = "Gallery", 
                       action = "Display" , 
                       layout = UrlParameter.Optional
                });

This is how my GalleryController.cs looks like:

public class GalleryController : Controller
    {
        // GET: Gallery
        public ActionResult   Display()
        {

            return View("Index");
        }

        public ActionResult Admin()
        {    
            return View();
        }


        public ActionResult Gifs()
        {
            if(Request.Url.AbsoluteUri.Contains("gifs/slide"))
                return View("GifsSlide");
            else
                return View("GifsGrid");
        }

        public ActionResult Still()
        {
            if (Request.Url.AbsoluteUri.Contains("stil/slide"))
                return View("StillSlide");
            else
                return View("StillGrid");
        }
    }

Inside my Gallery folder in Views I have following .cshtml structure

Views/Gallrey/gifsgrid.cshtml
Views/Gallrey/gifsslide.cshtml
Views/Gallrey/stillgrid.cshtml
Views/Gallrey/stillslide.cshtml
Views/Gallrey/admin.cshtml

Achievement:

localhost/gallery1/still/grid
localhost/gallery1/still/slide
localhost/gallery1/gifs/grid
localhost/gallery1/gifs/slide
localhost/gallery1/admin

Upvotes: 2

Related Questions