nubm
nubm

Reputation: 1153

ASP.NET MVC3 route for static .cshtml files

I'm adding some .cshtml files with some content (nothing dynamicaly loaded, just a static content)

There are several files: /Views is a directory /Dealership is a directory in /Views

Views - Dealership - About.cshtml
Views - Dealership - Testimonials.cshtml
Views - Dealership - Audi.cshtml
Views - Dealership - AudiA6.cshtml
Views - Dealership - AudiA8.cshtml
Views - Dealership - BMW.cshtml
Views - Dealership - BMW5.cshtml
Views - Dealership - BMW7.cshtml

Urls should be:

www.mywebsite.com/dealership/about
www.mywebsite.com/dealership/testimonials
www.mywebsite.com/dealership/audi
www.mywebsite.com/dealership/audi/audi-A6
www.mywebsite.com/dealership/audi/audi-A8
www.mywebsite.com/dealership/bmw
www.mywebsite.com/dealership/bmw/bmw-5

How the route should look like? I have this:

    routes.MapRoute(
        "Dealership", // Route name
        "dealership/{action}/{id}", // URL with parameters
        new { controller = "Dealership", action = "Index", id = string.Empty }); // Parameter defaults

It works for

www.mywebsite.com/dealership/audi

or

www.mywebsite.com/dealership/testimonials

but I don't know how to create the route for

www.mywebsite.com/dealership/audi/audi-A6

I hope it's not too confusing ;-)

Upvotes: 1

Views: 2820

Answers (2)

tvanfosson
tvanfosson

Reputation: 532755

Generally static content should go in the Content directory, but I can see why you don't want to do that. I would consider using partial views for the specific vehicles, then using logic in the base view for that manufacturer to determine whether to show the generic code or the partial for a particular view based on the model. In your controller, add another parameter for the car model (note, I've renamed id to make).

Route

routes.MapRoute(
        "Dealership", // Route name
        "dealership/{action}/{make}/{model}", // URL with parameters
        new { controller = "Dealership", action = "Index", make = string.Empty , model = UrlParameter.Optional }); // Parameter defaults

Controller

public ActionResult Index( string make, string model )
{
     return( make, model );
}

Views (audi.cshtml)

 @model string
 @if (string.IsNullOrEmpty(model)) {
    .. manufacturer html...
 }
 else
 {
    @Html.Partial( "audi-" + Model );
 }

Then have your view folder structured like

 dealership/audi.cshtml
 dealership/audi-audi-a6.cshtml
 ...

Upvotes: 2

Faber
Faber

Reputation: 2194

it's not working beacuse the framework search for a view with "audi-A6" name, but it doesn't exist. It's name is "audiA6". Try to change the view name in "audi-A6.cshtml".

I hope it's helpful

Upvotes: 0

Related Questions