Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 Dynamic Controller Path?

I have a SiteController class in my MVC4 project,the 'Site' url is working fine, but I need a dynamic url part right after 'Site', I need the url to look like this:

mysite.com/Site/{DYNAMICSTRING}/Users(or whatever)

{DYNAMICSTRING} can be the name of a subsite, so the controller should check if that subsite does actually exist in the database.

Right now I'm using Query strings, but that is not what my client wants.

How can I do that?

Additional details

My routing:

    routes.MapRoute(
                "Subdomain",                                              // Route name
                "{controller}/{action}/{dynamicString}",                           // URL with parameters
                new { controller = "Site", action = "Subdomain" }  // Parameter defaults
            );

My controller:

    public ActionResult Subdomain(string dynamicString)
    {
        return View();
    }

the value of dynamicString is null when I navigate to: /Site/Subdomain/SomeString

like image 234
jjdev80 Avatar asked Aug 12 '26 03:08

jjdev80


2 Answers

You have to configure routing. For example if you have Homecontroller:

    public class HomeController:Controller
    {
        public ActionResult Subdomain(string dynamicString)
        {
            return View();
        }
    }

then you have to configure your routing something like that

routes.MapRoute(
            "Subdomain",                                              // Route name
            "{controller}/{action}/{dynamicString}/anyOtherParams",                           // URL with parameters
            new { controller = "Home", action = "Subdomain", dynamicString = "" }  // Parameter defaults
        );
like image 96
Radislav Avatar answered Aug 14 '26 16:08

Radislav


You can do it like this:

routes.MapRoute(
            name: "Default", // Route name
            url:"Site/{dynamicstring}", // URL with parameters
            defaults: new {controller = "Site", action = "Index" }   // Defaults
        );

you can keep adding parts to the url part like so

 url:"Site/{dynamicstring}/{anythingelse}" //  url:"Site/{dynamicstring}/{anythingelse}/{bla}/Test/{hello}/etc..."

or you can also have a catch all route like this:"

routes.MapRoute(
            name: "Default", // Route name
            url:"{*all}", // catch all
            defaults: new {controller = "Site", action = "Index" }   // Defaults
        );

and fetch all other parts in your controllers index action, by splitting them on /

Make sure you put the custom route before your default otherwise the default route will pick it up.

in your controller you get something like this:

public ActionResult Index(string dynamicstring, string anythingelse)
    {

        return View();
    }

and if you then pass in a url like this:

http://www.mysite.com/Site/test.nl/jordy

your dynamicstring will have the value "test.nl" and your anythingelse will have "jordy" I hope this helps

like image 43
Jordy van Eijk Avatar answered Aug 14 '26 18:08

Jordy van Eijk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!