【发布时间】:2017-04-14 12:34:52
【问题描述】:
我只是在创建一个数据库驱动的 Web 应用程序。为此,我创建了一个控制器:
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; }
}
并在视图中显示数据:
@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 更友好,考虑到所有页面都是通过相同的控制器和操作加载的(每个 pageId 显示不同的数据),除了两个使用不同操作的页面同一个控制器。我希望链接看起来像
www.somewebsite.com/News/54
www.somewebsite.com/ContactUs
www.somewebsite.com/AboutUs
是否有可能,如果有的话,我该如何实现它?
【问题讨论】:
标签: asp.net-mvc model-view-controller url-rewriting asp.net-mvc-5 url-routing