静态 .cshtml 文件的 ASP.NET MVC3 路由

发布于 2024-11-12 18:32:13 字数 1228 浏览 0 评论 0原文

我添加了一些带有一些内容的 .cshtml 文件(没有动态加载,只是静态内容)

有几个文件: /Views 是一个目录 /Dealership 是 /Views 中的一个目录

Views - Dealership - About.cshtml
Views - Dealership - Testimonials.cshtml
Views - Dealership - Audi.cshtml
Views - Dealership - AudiA6.cshtml
Views - Dealership - AudiA8.cshtml
Views - Dealership - BMW.cshtml
Views - Dealership - BMW5.cshtml
Views - Dealership - BMW7.cshtml

网址应该是:

www.mywebsite.com/dealership/about
www.mywebsite.com/dealership/testimonials
www.mywebsite.com/dealership/audi
www.mywebsite.com/dealership/audi/audi-A6
www.mywebsite.com/dealership/audi/audi-A8
www.mywebsite.com/dealership/bmw
www.mywebsite.com/dealership/bmw/bmw-5

路线应该是什么样子?我有这个:

    routes.MapRoute(
        "Dealership", // Route name
        "dealership/{action}/{id}", // URL with parameters
        new { controller = "Dealership", action = "Index", id = string.Empty }); // Parameter defaults

它适用于

www.mywebsite.com/dealership/audi

www.mywebsite.com/dealership/testimonials

但我不知道如何创建 的路线

www.mywebsite.com/dealership/audi/audi-A6

我希望它不会太混乱;-)

I'm adding some .cshtml files with some content (nothing dynamicaly loaded, just a static content)

There are several files:
/Views is a directory
/Dealership is a directory in /Views

Views - Dealership - About.cshtml
Views - Dealership - Testimonials.cshtml
Views - Dealership - Audi.cshtml
Views - Dealership - AudiA6.cshtml
Views - Dealership - AudiA8.cshtml
Views - Dealership - BMW.cshtml
Views - Dealership - BMW5.cshtml
Views - Dealership - BMW7.cshtml

Urls should be:

www.mywebsite.com/dealership/about
www.mywebsite.com/dealership/testimonials
www.mywebsite.com/dealership/audi
www.mywebsite.com/dealership/audi/audi-A6
www.mywebsite.com/dealership/audi/audi-A8
www.mywebsite.com/dealership/bmw
www.mywebsite.com/dealership/bmw/bmw-5

How the route should look like? I have this:

    routes.MapRoute(
        "Dealership", // Route name
        "dealership/{action}/{id}", // URL with parameters
        new { controller = "Dealership", action = "Index", id = string.Empty }); // Parameter defaults

It works for

www.mywebsite.com/dealership/audi

or

www.mywebsite.com/dealership/testimonials

but I don't know how to create the route for

www.mywebsite.com/dealership/audi/audi-A6

I hope it's not too confusing ;-)

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

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

发布评论

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

评论(2

挽清梦 2024-11-19 18:32:13

一般来说,静态内容应该放在 Content 目录中,但我可以理解为什么你不想这样做。我会考虑对特定车辆使用部分视图,然后使用该制造商的基本视图中的逻辑来确定是否显示通用代码或基于模型的特定视图的部分。在控制器中,为汽车模型添加另一个参数(注意,我已将 id 重命名为 make)。

路线

routes.MapRoute(
        "Dealership", // Route name
        "dealership/{action}/{make}/{model}", // URL with parameters
        new { controller = "Dealership", action = "Index", make = string.Empty , model = UrlParameter.Optional }); // Parameter defaults

控制器

public ActionResult Index( string make, string model )
{
     return( make, model );
}

视图(audi.cshtml)

 @model string
 @if (string.IsNullOrEmpty(model)) {
    .. manufacturer html...
 }
 else
 {
    @Html.Partial( "audi-" + Model );
 }

然后让您的视图文件夹结构如下

 dealership/audi.cshtml
 dealership/audi-audi-a6.cshtml
 ...

Generally static content should go in the Content directory, but I can see why you don't want to do that. I would consider using partial views for the specific vehicles, then using logic in the base view for that manufacturer to determine whether to show the generic code or the partial for a particular view based on the model. In your controller, add another parameter for the car model (note, I've renamed id to make).

Route

routes.MapRoute(
        "Dealership", // Route name
        "dealership/{action}/{make}/{model}", // URL with parameters
        new { controller = "Dealership", action = "Index", make = string.Empty , model = UrlParameter.Optional }); // Parameter defaults

Controller

public ActionResult Index( string make, string model )
{
     return( make, model );
}

Views (audi.cshtml)

 @model string
 @if (string.IsNullOrEmpty(model)) {
    .. manufacturer html...
 }
 else
 {
    @Html.Partial( "audi-" + Model );
 }

Then have your view folder structured like

 dealership/audi.cshtml
 dealership/audi-audi-a6.cshtml
 ...
盛装女皇 2024-11-19 18:32:13

它不起作用,因为框架搜索名称为“audi-A6”的视图,但它不存在。它的名字是“audiA6”。
尝试更改“audi-A6.cshtml”中的视图名称。

我希望它有帮助

it's not working beacuse the framework search for a view with "audi-A6" name, but it doesn't exist. It's name is "audiA6".
Try to change the view name in "audi-A6.cshtml".

I hope it's helpful

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