本文介绍了使用的VirtualPathProvider加载DLL文件从ASP.NET MVC意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这里这个问题我试图加载嵌入资源的看法一个单独的DLL项目,原题的作者说,他已经获得了成功这样做 - 但我不能得到它的工作,因为它似乎MVC视图引擎是截取了请求,仍然看视图的文件系统。例外:

 在'/'应用程序的服务器错误。
视图索引或它的主人找不到。在以下地点被搜查:
〜/查看/管理/的Index.aspx
〜/查看/管理/ Index.ascx
〜/查看/共享/的Index.aspx
〜/查看/共享/ Index.ascx
〜/应用/浏览/管理/的Index.aspx
〜/应用/浏览/管理/ Index.ascx
〜/应用/查看/共享/的Index.aspx
〜/应用/查看/共享/ Index.ascx

我使用的是 CustomViewEngine ,像罗布·康纳利的/应用程序结构中的一个如下:

 公共类CustomViewEngine:WebFormViewEngine
    {
         公共CustomViewEngine()
         {
             MasterLocationFormats =新[] {
                〜/应用/查看/ {1} / {0}的.master
                〜/应用/查看/共享/ {0}的.master
                };             ViewLocationFormats =新[] {
                〜/应用/查看/ {1} / {0}的.aspx
                〜/应用/查看/ {1} / {0}的.ascx
                〜/应用/查看/共享/ {0}的.aspx
                〜/应用/查看/共享/ {0}的.ascx
                };             PartialViewLocationFormats = ViewLocationFormats;
         }
    }

下面是我的路线:

  routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);    routes.MapRoute(家,,新{控制器=页面,行动=索引,ID =默认});
    routes.MapRoute(默认,页/(编号),新{控制器=页面,行动=索引,ID =});
    routes.MapRoute(插件,插件/ {控制器} / {行动},新{控制器=,行动=索引,ID =});
    routes.MapRoute(错误,{* URL},新{控制器=错误,行动=ResourceNotFound404});

在我的我检查,看是否路径启动〜/插件/ ,然后使用该dll文件名约定插件。{}控制器.DLL

有什么建议?

更新:通过对发言权路由请求 HTTP时间://本地主机/插件/系统管理员越来越向VirtualFileProvider它doesn'吨有附加在最后的视图。所以在的 VirtualFileProvider Open方法的虚拟路径〜/插件/系统管理员正在传递的时候应该为〜/插件/管理/的Index.aspx 在我的路线上面定义。难道我搞砸了我的路线还是我的权利在期待这样的事情发生?


解决方案

  1. 您必须注册的VirtualPathProvider 的Global.asax 的Application_Start 处理程序。

  2. 返回查看(〜/插件/ YOURDLL.dll / FULLNAME_YOUR_VIEW.aspx):
  3. 您使用像这样的特殊路径必须调用您的DLL视图/ LI>

下面是可下载code示例演示此文章:

Based on this question here and using code found here I'm trying to load views that are embedded resources in a separate DLL project, and the original question's author says he has had success doing this - but I can't get it to work as it seems the MVC view engine is intercepting the request and still looking at the file system for the view. Exception:

Server Error in '/' Application.
The view 'Index' or its master could not be found. The following locations were searched:
~/Views/admin/Index.aspx
~/Views/admin/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/App/Views/admin/Index.aspx
~/App/Views/admin/Index.ascx
~/App/Views/Shared/Index.aspx
~/App/Views/Shared/Index.ascx

I am using a CustomViewEngine, like Rob Connery's /App structure one as follows:

public class CustomViewEngine : WebFormViewEngine
    {
         public CustomViewEngine()
         {
             MasterLocationFormats = new[] {
                "~/App/Views/{1}/{0}.master",
                "~/App/Views/Shared/{0}.master"
                };

             ViewLocationFormats = new[] {
                "~/App/Views/{1}/{0}.aspx",
                "~/App/Views/{1}/{0}.ascx",
                "~/App/Views/Shared/{0}.aspx",
                "~/App/Views/Shared/{0}.ascx"
                };

             PartialViewLocationFormats = ViewLocationFormats;
         }
    }

Here are my routes:

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute("Home", "", new {controller = "Page", action = "Index", id = "Default"});
    routes.MapRoute("Default", "Page/{id}", new { controller = "Page", action = "Index", id = "" });
    routes.MapRoute("Plugins", "plugin/{controller}/{action}", new { controller = "", action = "Index", id = "" });
    routes.MapRoute("Error", "{*url}", new { controller = "Error", action = "ResourceNotFound404" });

In my AssemblyResourceProvider I'm checking to see if the path starts ~/plugin/ and then using the dll filename convention plugin.{controller}.dll

Any suggestions?

UPDATE: By the time the routed request for say http://localhost/plugin/admin is getting to the VirtualFileProvider it doesn't have any View attached at the end. So in the VirtualFileProvider's Open method the virtual path of ~/plugin/admin is being passed in when it should be ~/plugin/admin/Index.aspx as defined in my route above. Have I messed up my routes or am I right to be expecting this to happen?

解决方案
  1. You must register your VirtualPathProvider within the Global.asax Application_Start handler.
  2. You must call the view in your DLL using the special path like so: return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");

Here's an article with downloadable code sample that demonstrates this:

http://www.wynia.org/wordpress/2008/12/aspnet-mvc-plugins/

这篇关于使用的VirtualPathProvider加载DLL文件从ASP.NET MVC意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:55