Redirecting to the controller (but with a different wizard) using the catchall pattern

I have a problem where I want to display the view in a different way (another main page), depending on where it came from, but don’t know where to start ...

I have several routes that capture different types of URLs that contain different structures.

In the code snippet below, I have a product route, and then I have a partner route, which can also go to the product page, but let them say that this partner is Pepsi, and they want their branding on the main page, and not our own default style. Suppose I go to products / cola.htm . This should go to the same URL as partners / pepsi / products / cola.htm , and PartnerRedirect will be able to process the URL based on the pattern by translating the url (in this case, “products / cola.htm”) to controller action and redirect the user (but just change the main page in the view).

routes.MapRoute(
    "Product",
    "products/{product}.htm",
    new { controller = "Product", action = "ShowProduct" }
);

routes.MapRoute(
    "ProductReview",
    "products/{product}/reviews.htm",
    new { controller = "Product", action = "ShowProductReview" }
);

routes.MapRoute(
    "Partner",
    "partners/{partner}/{*wildcard}",
    new { controller = "Partners", action = "PartnerRedirect" }
);

Is it possible? And if so, how?

Thank you very much in advance.

+3
source share
6

, cookie, , , . , .

, , , .

+1

public class FriViewPage : ViewPage
{
    public override string MasterPageFile
    {
        get
        {
            return "~/Views/Shared/Site.Master"; // base.MasterPageFile;
        }
        set
        {
            if (ViewData["agent"].ToString() == "steve")
                base.MasterPageFile = "~/Views/Shared/Site.Master";
            else
                base.MasterPageFile = "~/Views/Shared/Site2.Master";
        }
    }
}

, FriViewPage ViewPage

+1

, , , URL-, ?

0

, , , , (, , ).

, .

0

In fact, the recipient of the MasterPageFile is never called

0
source

You can change the MasterPage by changing the ViewResult before rendering. For example, a controller action may be performed:

public ActionResult TestMP(int? id)
{
    ViewData["Title"] = "MasterPage Test Page";
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    ViewResult result = View("Index");
    if (id.HasValue)
    {
        result.MasterName = "Site2";
    }
    return result;
}

You can do the same with an action filter for a more general solution.

0
source

Source: https://habr.com/ru/post/1698717/


All Articles