使用包罗万象的通配符重定向到控制器(但使用不同的主控制器)

发布于 2024-07-08 02:32:34 字数 1007 浏览 8 评论 0原文

我有一个问题,我想根据它的来源以不同的方式显示视图(不同的母版页),但不知道从哪里开始...

我有几条路线可以捕获包含的各种不同类型的网址不同的结构。

在下面的代码片段中,我有一个产品路线,然后有一个合作伙伴网站路线,也可以转到产品页面,但假设该合作伙伴是百事可乐,他们希望在母版页上展示自己的品牌,而不是我们自己的默认样式。 假设我访问 products/cola.htm。 这应该转到与 partners/pepsi/products/cola.htm,并且 PartnerRedirect 将能够通过将 url 通配符(在本例中为“products/cola.htm”)转换为控制器操作来处理基于通配符的 url,并将用户转发到(但只需更改视图中的母版页)。

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" }
);

这可能吗? 如果是这样,怎么办?

提前谢谢了。

I have a problem whereby I want to display a view differently (a different master page), depending on where it came from, but don't know where to start...

I have several routes which catch various different types of urls that contain different structures.

In the code snippet below, I have a product route, and then I have a partner site route which could also go to a product page, but let's say that this partner is Pepsi, and they want their branding on the master page, rather than our own default styling. Lets say I go to products/cola.htm. This should go to the same url as partners/pepsi/products/cola.htm, and the PartnerRedirect would be able to handle the url based on the wildcard, by translating the url wildcard (in this case, "products/cola.htm") into a controller action, and forward the user on, (but simply change the master 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 this possible? And if so, how?

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挖鼻大婶 2024-07-15 02:32:34

在您的合作伙伴控制器中,为什么不设置一个 cookie 来指示您想要显示哪个合作伙伴,然后重定向到路由的通配符部分。 这样您就可以为所有后续页面视图显示相同的合作伙伴布局。

我不知道这是否是您正在寻找的,但这可能是一个选择。

In your partners controller why don't you set a cookie that indicates which partner you want to show, and then redirects to the wildcard section of the route. That way you can show the same partner layout for all subsequent page views.

I don't know if this is what you're looking for, but it might be an option.

流年已逝 2024-07-15 02:32:34

我有同样的问题

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

I had same issue

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";
        }
    }
}

Then just ensure all the views inherit from FriViewPage instead of ViewPage

葬シ愛 2024-07-15 02:32:34

这可能是魔鬼的工作,但您可以在合作伙伴视图的代码隐藏中放置一些代码来查看 URL,然后以编程方式在那里设置母版页?

It may be the devils work but you could put some code in the Partner View's codebehind to look at the URL and then set the master page programmatically in there?

静若繁花 2024-07-15 02:32:34

我不确定如何以编程方式更改母版页,因为我从未这样做过,但我确信这是可能的(它可能只是页面上的一个属性)。

这可能值得作为另一个问题来问。

I'm not sure how you can programatically alter the master page, as I've never done that, but I'm sure it's possible (it's probably just a property on Page).

That might be worth asking as another question.

烈酒灼喉 2024-07-15 02:32:34

实际上 MasterPageFile getter 似乎从未被调用

Acutally the MasterPageFile getter never appears to be called

葬花如无物 2024-07-15 02:32:34

您可以通过在渲染之前修改 ViewResult 来更改 MasterPage。 例如,控制器操作可以执行以下操作:

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 change the MasterPage by modifying the ViewResult prior to rendering. For example, a controller action could do:

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 could accomplish the same thing with an action filter for a more generic solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文