静态 .cshtml 文件的 ASP.NET MVC3 路由
我添加了一些带有一些内容的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,静态内容应该放在 Content 目录中,但我可以理解为什么你不想这样做。我会考虑对特定车辆使用部分视图,然后使用该制造商的基本视图中的逻辑来确定是否显示通用代码或基于模型的特定视图的部分。在控制器中,为汽车模型添加另一个参数(注意,我已将
id
重命名为make
)。路线
控制器
视图(audi.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
tomake
).Route
Controller
Views (audi.cshtml)
Then have your view folder structured like
它不起作用,因为框架搜索名称为“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