up vote 1 down vote favorite
1

Say I have 3 versions of a website: A, B and C. I want my urls to be of the form:

{siteType}/{controller}/{action}/{id}

where siteType equals a, b or c.

When the user is in the A version of the website, then they should stay there; therefore all generated urls should have a siteType of a. Similarly for B and C.

I don't want to have to explicitly specify the siteType when generating urls - I want it generated automatically. Furthermore, the siteType parameter will only ever be used in one place - the overrided RenderView method in a base controller class - which will use the siteType parameter to pick out the correct view, css etc for that version of the website. I therefore have no interest in the siteType appearing as an argument to my actions. Only the RenderView method requires access to it.

What is the best way to achieve this?

flag

2 Answers

up vote 1 down vote accepted

We have almost the same with the site language (snippet from our global.asax.cs):

routes.MapRoute(
            "DefaultLanguage",
            "{lang}/{controller}/{action}/{id}",
            new { lang = "de-CH", controller = "Home", action = "Index", id = "" }
        );

Whenever no language is set the language will be set to swiss-german in our case.

Any actionlink will have the language code automatically from the current site. So we don't have to specify any language on Actionlinks.

Changing the siteType is simple, just add routeValues to your actionlink. e.g.

 <%= Html.ActionLink("Linktext", "Action", "Controller", new { siteType = "b" } %>
link|flag
Yes, but the point is that I don't want to have to add siteType to every ActionLink or to any other url that is generated. Since it is done everywhere, I should be able to have it added automatically in a single place. – darasd Aug 27 '09 at 8:27
you don't have to add siteType to every url by "hand". Set it once in default routine and it will be added. If you would like to switch between the siteTypes, then you have to add siteType to that action link. All my links are like: <%= Html.ActionLink("Linktext", "Action", "Controller" %> except the one that is switching the language. ActionLink renders the link and adds siteType automatically to something like: <a href="a/Home/About">Linktext</a> – griti Aug 27 '09 at 8:56
You know, I think you're right! It appears to be added automatically, and to take the current url's value rather than the default if not added explicitly to, for instance, an ActionLink. – darasd Aug 27 '09 at 10:15
up vote 0 down vote

I may be quite off base but what it sounds like to me is a mobile site a-la

http://weblogs.asp.net/mschwarz/archive/2007/11/26/mvc-framework-and-mobile-device.aspx

public class MobileCapableWebFormViewEngine : WebFormViewEngine { public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { ViewEngineResult result = null; var request = controllerContext.HttpContext.Request;

    // Avoid unnecessary checks if this device isn't suspected to be a  mobile device
    if (request.Browser.IsMobileDevice)
    {
        result = base.FindView(controllerContext, "A/" + viewName, masterName, useCache);
    }

    //Fall back to desktop view if no other view has been selected
    if (result == null || result.View == null)
    {
        result = base.FindView(controllerContext, viewName, masterName, useCache);
    }

    return result;
} }

You could also instead of

Request.Browser.IsMobileDevice

have

Request.ServerVariables["URL"].Contains("A")

link|flag
Yes, this solves the problem of getting the correct view, but it does not solve the problem of having the siteType ("a") automatically added to each url. – darasd Aug 27 '09 at 9:10

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.