ASP .NET MVC-3 路由添加/映射到别名的虚拟目录
我关注了 Maarten Balliauw 的帖子域路由。我已经能够路由到不同子域的控制器。但我不知道如何路由到虚拟目录。正如您所看到的,这是路由到常用 mvc 控制器的示例,
routes.Add("DomainRoute", new DomainRoute(
"home.example.com", // Domain with parameters
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
));
由于我的博客驻留在虚拟目录上,我需要将路由添加到名为“~/blog”的虚拟目录 我已经尝试过这样的代码,但没有好的结果,
routes.Add("DomainRoute", new DomainRoute(
"blog.domain.com", // Domain with parameters
"blog", // URL with parameters
new { controller = "blog" } // Parameter defaults
));
如果有人能对此带来一些启发,那就太棒了。
I have followed Maarten Balliauw's post on domain routing. I had been able route to controller's for different sub-domains. But I don't know how to route to a virtual directory. As you see this is a sample example for routing to usual mvc controllers,
routes.Add("DomainRoute", new DomainRoute(
"home.example.com", // Domain with parameters
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
));
As my blog resides on Virtual Directory I need to add the route to this virtual directory named "~/blog"
I have tried code like this with no good result,
routes.Add("DomainRoute", new DomainRoute(
"blog.domain.com", // Domain with parameters
"blog", // URL with parameters
new { controller = "blog" } // Parameter defaults
));
If anyone can bring some light into this it will be amazing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试将博客设为主 Web 应用程序的 MVC 区域。您创建一个名为博客的新区域。这将在您的主 webroot 中创建一个名为“Areas/blog”的文件夹。然后,您可以将“blog”文件夹转换为指向您的博客应用程序的虚拟目录。
我在最近的一个管理后端网络项目中这样做了。我使用了这篇博客文章,效果很好。非常简单,可以让您的应用程序保持美观和干净。
http://bob.archer.net/content/aspnet-mvc3-areas -separate-projects
当您实际上位于“博客”区域内时,您的操作链接和一切都会正常工作。您只需将“Area = 'blog'”添加到您的链接中,即可将您从一个区域移动到另一个区域。
创建博客区域后,请查看它创建的路由文件。我希望这是有道理的。
You could try to make the blog an MVC Area of your main web application. You create a new Area called blog. This creates a Folder called "Areas/blog" in your main webroot. You can then turn the 'blog' folder into a virtual directory pointing to your blog application.
I did this in a recent web project for my admin backend. I used this blog post and it worked great. Very simple and keeps your application nice and clean.
http://bob.archer.net/content/aspnet-mvc3-areas-separate-projects
When you are actually inside the 'blog' area your actionlinks and everything works like normal. You only need to add "Area = 'blog'" to your links that move you from area to area.
Once you create the blog area take a look at the routes file it creates. I hope that makes sense.
这里的含义是,虚拟文件夹与它所在的 MVC 应用程序完全不同——如果是这种情况,您是否需要使用 MVC 路由,因为您一开始就不需要任何 MVC 资源?只需将子域配置为指向 IIS 中的相应文件夹,并完全绕过 MVC 的路由,可能会更容易。
The implication here is that the virtual folder is completely distinct from the MVC application it's within -- if that's the case, do you need to use the MVC routing at all, since you don't need any of the MVC resources to begin with? It would probably be easier just to configure the subdomain to point at the appropriate folder within IIS, and bypass MVC's routing altogether.