gpt4 book ai didi

asp.net-mvc - 如何在 MVC 5 中为动态填充的页面重写 URL

转载 作者:行者123 更新时间:2023-12-05 01:22:39 26 4
gpt4 key购买 nike

我只是在创建一个数据库驱动的 Web 应用程序。为此,我创建了一个 Controller :

public class PagesController : Controller
{
private StoreDB db = new StoreDB();

public ActionResult LoadOneColumnPageContent(string pageId)
{
List<tbl_UserControl> tbl_UserControls = db.tbl_UserControl.Where(it => it.Active == true && it.PageId == pageId).OrderBy(it => it.Id).ToList();
return View(tbl_UserControls);
}
public ActionResult LoadFullWidthPageContent(int id)
{
List<tbl_UserControl> tbl_UserControls = db.tbl_UserControl.Where(it => it.Id == id).ToList();
return View(tbl_UserControls);
}
}

填充模型

public partial class tbl_UserControl
{
private int? _priority;

[Key]
public int Id { get; set; }

[AllowHtml]
public string Title { get; set; }

[AllowHtml]
public string Subtitle { get; set; }

[AllowHtml]
[DataType(DataType.MultilineText)]
public string MainText { get; set; }

[StringLength(20)]
public string ButtonText { get; set; }

public string ImageUrl { get; set; }

public string UrlLink { get; set; }

public string PageId { get; set; }

public string TypeId { get; set; }
}

并在 View 中显示数据:

    @using Microsoft.Ajax.Utilities
@model IEnumerable<newSiteMVC.Models.tbl_UserControl>
@{
ViewBag.Title = "LoadOneColumnPageContent";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@*<%--------------------- Top Banner with moving image ---------------------%>*@

@Html.Partial("~/Views/Pages/Shared/_PageTopBanner.cshtml", Model)

@*<%--------------------- Main page content ---------------------%>*@

@foreach (var item in Model.Where(it => it.TypeId != "NewsFeed" && it.PageId == @Request.QueryString["pageId"]))
{
if ( item.TypeId != "TopBanner" && item.TypeId != "TwoColumn" && item.TypeId != "VideoPod")
{
if (string.IsNullOrEmpty(item.ImageUrl))
{
@Html.Partial("~/Views/Pages/Shared/_OneColumnComponent.cshtml", item)
}
else
{
@Html.Partial("~/Views/Pages/Shared/_OneColumnWithImageComponent.cshtml", item)
}
}
}
@if (@Request.QueryString["pageId"] == "InTheNews")
{
@Html.Partial("~/Views/Pages/Shared/_OneColumnWithLinkComponent.cshtml", Model)

}

@if (@Request.QueryString["pageId"] == "ContactUs")
{
@Html.Partial("~/Views/Pages/Shared/_FullWidthContactPage.cshtml", Model)

}

@if (@Request.QueryString["pageId"] == "ShoppingCart")
{
@Html.Partial("~/Views/Pages/Shared/_TwoColumnComponent.cshtml", Model)

}

@*<%--------------------- Moving Background Image ---------------------%>*@

@Html.Partial("_MovingBackgroundImage")

此时 Web 应用程序显示以下两个 url 模式:

http://localhost:63294/Pages/LoadFullWidthPageContent/54

或者

http://localhost:63294/Pages/LoadOneColumnPageContent?pageId=ContactUs
http://localhost:63294/Pages/LoadOneColumnPageContent?pageId=AboutUs

我想知道是否有任何方法可以使 URL 对 SEO 更友好,考虑到所有页面都是通过相同的 Controller 和操作加载的(每个 pageId 显示不同的数据)除了两个页面在同一个 Controller 。我希望链接看起来像

 www.somewebsite.com/News/54
www.somewebsite.com/ContactUs
www.somewebsite.com/AboutUs

是否有可能,如果有的话,我有什么想法可以实现吗?

最佳答案

这不是完整的答案,但试图给你一个提示,因为我在类似的路线上工作,使页面对 SEO 更友好。如果对您有帮助,您可以利用。

         var route1 = routes.MapRoute(
name: "Products",
url: "Clothes/{param1}/{param2}",
defaults: new { controller = "Products", action = "Clothes", param1 = UrlParameter.Optional, param2 = UrlParameter.Optional }
);

这里Param1和Param2是可选的,你可以在那里添加任何东西。

就像你的情况一样,你可以添加

www.somewebsite.com/content/News/54

         var route1 = routes.MapRoute(
name: "Page",
url: "Content/{param1}/{param2}",
defaults: new { controller = "Page", action = "LoadOneColumnPageContent", param1 = UrlParameter.Optional, param2 = UrlParameter.Optional }
);

关于asp.net-mvc - 如何在 MVC 5 中为动态填充的页面重写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40884095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com