我有一个使用 WebForms .Net 4.0 的解决方案。我计划在同一个解决方案中使用 MVC3。我关注了 Scott Hanselman的博客,事情进展顺利。
我不得不承认我对此很陌生。但是,我似乎错过了路由在命名空间方面的真正工作方式的很大一部分。
目前,我们的解决方案有:
WebApplicatin:
Accounting
Receivables
ReceivablesGrid.aspx
ReceivableForm.aspx
Payables
PayablesGrid.aspx
PayablesForm
..etc.
因此,您可以使用请求页面
Domain/Accounting/Receivables/ReceivablesGrid.aspx
Domain/Accounting/Receivables/ReceivableForm.aspx?Key=1
Domain/Accounting/Payables/PayablesGrid.aspx
Domain/Accounting/Payables/PayablesForm.aspx?Key=1
……
我打算添加另一层来类似于 MVC。
WebApplicatin:
Accounting
Receivables
ReceivablesGrid.aspx
ReceivableForm.aspx
Mobile
Controllers
ReceivableConroller.cs
Models
Views
Receivables
Index
Update
Edit
Create
Payables
PayablesGrid.aspx
PayablesForm
Mobile
Controllers
PayablesConroller.cs
Models
Views
Payables
Index
Update
Edit
Create
..etc.
当然,这不是真名。但是,我试图让它尽可能接近我的情况。不幸的是,最好遵循这一点,因为我可以使用一些可能添加到同一名称空间的逻辑。此外,在根目录创建类似于控制器、视图、模型的文件夹不适用于我的解决方案。
在 Global.asax 中,我添加了这样一条路线:
routes.MapRoute(
"AccountingReceivablesMobile", // Route name
"Accounting/Receivables/Mobile/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"AccountingPayablesMobile", // Route name
"Accounting/Payables/Mobile/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
我最终尝试的另一个解决方案是扩展 RazorViewEngine。在新引擎的构造函数中,我设置了两个属性如下:
base.ViewLocationFormats = new string[] { "~/Accounting/Receivables/Mobile/Views/{1}/{0}.cshtml",
"~/Accounting/Payables/Mobile/Views/{1}/{0}.cshtml"
};
base.MasterLocationFormats = new[] { "~/Views/Shared/{0}.cshtml"}.
这工作得很好。但是,我只是觉得添加这些路由不像添加 webForm 那样可扩展。我的问题是我真的不想为每条可能的路线添加路线。这意味着,当我添加新视图时,我将有另一个路由或数组条目。那么,我怎样才能让这更简单?我究竟做错了什么?我查看了 Areas,但它似乎强制创建一个 Areas 文件夹并将其放入其中。
谢谢,