基于Spring.NET的MVC应用程序发布时的虚拟路径错误的解决方案
2013-03-11 11:09 GPS视频平台产品经理 阅读(1271) 评论(0) 编辑 收藏 举报使用spring.NET1.3以上版本时,如果web程序没有部署在根目录下,会出现虚拟路径的错误。错误如下:
虚拟路径“/saas2/currentcontext.dummy”映射到另一个应用程序,这是不允许的。
费力好长时间,都没有解决问题,跑到spring.NET论坛上才找到解决问题的方案,方案如下:
< spring > < strong > < context name="saas2" type="Spring.Context.Support.WebApplicationContext, Spring.Web"></ strong > < resource uri="file://~/Config/controller.xml"/> <!-- <resource uri="assembly://saas/saas.Config/Controller.xml"/> --> < resource uri="file://~//Dao_Saas.xml"/> < resource uri="assembly://saas.util/saas.Util.Config/QueryService.xml"/> < resource uri="assembly://saas.Service/saas.Config/Service.xml"/> </ context > </ spring > |
其实就是在Context的标签上写上你的虚拟路径。<context name="saas2" ></context>
如果要自动识别,而不是发布在不同的虚拟目录下都要设置一遍,就需要自己写一个ContextHandler类了,如下所示:
using System; using System.Xml; using System.Web; using System.Reflection; using Spring.Context.Support; using Common.Logging; namespace Spring.Context.Support { public class AutoNamedContextHandler : ContextHandler { protected ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); protected override string GetContextName( object configContext, XmlElement contextElement) { string res = base .GetContextName(configContext, contextElement); if ( string .IsNullOrEmpty(res) == false ) { logger.Info( "Using declared context name: " + res); return res; } string appPath = HttpContext.Current.Request.ApplicationPath; logger.Info( "Using discovered context name: " + appPath); return appPath; } } } |
然后将此类配置在web.config配置文件的spring节中就可以了。这样就可以自由发布了。
<?xml version= "1.0" ?> <configuration> <configSections> <sectionGroup name= "spring" > <section name= "context" type= "Spring.Context.Support.AutoNamedContextHandler, Tools" /> </sectionGroup> </configSections> <spring> <context type= "Spring.Context.Support.WebApplicationContext, Spring.Web" > <!-- ... --> </context> </spring> </configuration> |
我的微信号: jt808_com 添加微信
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架