Let's say I have a model called artikel. this article contains Html "body" text. and a one word title.
假设我有一个名为artikel的模型。本文包含Html“正文”文本。和一个单词的标题。
Then I want to make a system, where i can use one view to render all of "Article" model's body content.
然后我想制作一个系统,我可以使用一个视图来渲染所有“文章”模型的正文内容。
but use the articles Title prop. to create a Url for the site.
但请使用文章Title prop。为网站创建网址。
So if i got 2 articles. one titled "About", and one with the title "Contact"
所以,如果我有2篇文章。一个标题为“关于”,一个标题为“联系人”
i would end up with Url like "site/About" And "site/Contact"
我最终会像“网站/关于”和“网站/联系人”这样的网址
And since I'm trying to make this from a data source, I need some way to do this dynamic. so i can't just make controllers for each artikel. (Which would be bad if i got many articles anyways)
因为我试图从数据源中做出这个,所以我需要一些方法来做这个动态。所以我不能只为每个artikel制作控制器。 (如果我收到很多文章,这将是不好的)
I been trying in my RouteConfig to setup a mapRoute. but can't find anyway which would do this.
我一直在尝试在RouteConfig中设置mapRoute。但无论如何都找不到会这样做的。
I search the net for it, an tried those solutions.
我在网上搜索它,尝试了那些解决方案。
.Net MVC中的URL重写
https://www.youtube.com/watch?v=h405AbJyiH4
https://forums.asp.net/t/2094370.aspx?How+To+Create+URL+Rewrite+In+ASP+NET+C+using+MVC+
But no luck. Anyone that know's how to do this, or that can help me in the right direction.?
但没有运气。任何知道如何做到这一点的人,或者能帮助我朝着正确方向前进的人。
1 个解决方案
#1
0
There are many ways to solve this problem, for example you can set Artikel to be the default controller, creating new route, or a custom route, etc. I recommend to include artikel in the url website/artikel/pagename
for finding the articles.
有很多方法可以解决这个问题,例如你可以将Artikel设置为默认控制器,创建新路线或自定义路线等。我建议在网址/ artikel / pagename中包含artikel以查找文章。
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Artikel",
url: "artikel/{id}",
defaults: new { controller = "Artikel", action = "Index", id = UrlParameter.Optional }
);
//default and other routes go here, after Artikel
}
In Artikel controller:
在Artikel控制器中:
public ActionResult Index(string id)
{
Artikel model = Database.GetArticle(id);
return View(model);
}
Model:
public class Artikel
{
public string Title { get; set; }
public string Body { get; set; }
}
And the view:
并且观点:
@model MyApplication.Models.Artikel
@{
ViewBag.Title = "Index";
}
<h2>@Model.Title</h2>
<span>@Model.Body</span>
#1
0
There are many ways to solve this problem, for example you can set Artikel to be the default controller, creating new route, or a custom route, etc. I recommend to include artikel in the url website/artikel/pagename
for finding the articles.
有很多方法可以解决这个问题,例如你可以将Artikel设置为默认控制器,创建新路线或自定义路线等。我建议在网址/ artikel / pagename中包含artikel以查找文章。
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Artikel",
url: "artikel/{id}",
defaults: new { controller = "Artikel", action = "Index", id = UrlParameter.Optional }
);
//default and other routes go here, after Artikel
}
In Artikel controller:
在Artikel控制器中:
public ActionResult Index(string id)
{
Artikel model = Database.GetArticle(id);
return View(model);
}
Model:
public class Artikel
{
public string Title { get; set; }
public string Body { get; set; }
}
And the view:
并且观点:
@model MyApplication.Models.Artikel
@{
ViewBag.Title = "Index";
}
<h2>@Model.Title</h2>
<span>@Model.Body</span>