How to build embedded resources from MVC 3 and IIS7?

I am developing an MVC 3 application with plugin functionality. plugins are C # dll with all necessary resources (css, images and scripts).

I used the Visual Studio extension MvcRazorClassGenerator to create precompiled views.

I am extracting the inline resource using the following code:

public FileStreamResult EmbeddedResource(string pluginName, string resourceName) { Assembly assembly = PluginCache.GetAssembly(pluginName); if (assembly != null) { string tempResourceName = assembly.GetManifestResourceNames() .ToList().FirstOrDefault(f => f.EndsWith(resourceName)); return new FileStreamResult( assembly.GetManifestResourceStream(tempResourceName), GetMIMEType(tempResourceName)); } return null; } 

In the views, I have the following code for accessing resources:

 @Url.Content("/Common/EmbeddedResource/PluginName/[AssemblyNamespace].Content.Images.blank.gif") 

Everything works fine, while I am in the development environment, all resources are loaded and displayed correctly, but a nightmare starts during deployment.

IIS 7.5 continues to search for a static file named "/Common/EmbeddedResource/PluginName/[AssemblyNamespaceapter.Content.Images.blank.gif" rather than an embedded file, which gives me a 404 error for all the embedded resources.

I tried to install the hotfix mentioned by the question on this site and modify the configuration files, but the resources were not loaded.

I am trying to install on a 64-bit version of Windows Server 2008 R2 R2.

+4
source share
2 answers

The problem may be using

 Url.Content("/Common/EmbeddedResource/PluginName/[AssemblyNamespace].Content.Images.blank.gif") 

I have the same setup, but since the content is served from the action I'm using

 @Url.Action("EmbeddedResource", "EmbeddedResources", new { pluginName = "PluginName", resourceName = "MyProject.Scripts.MyScript.js" }) 

Then I set the route

 routes.MapRoute( "EmbeddedResources", "EmbeddedResources/{pluginName}/{resourceName}", new { controller = "EmbeddedResources", action = "EmbeddedResource", pluginName = "DefaultPluginName", resourceName = UrlParameter.Optional }); 

What leads to such script links

 <script type='text/javascript' src='/EmbeddedResources/PluginName/MyProject.Scripts.MyScript.js'></script> 

If you take this approach, you can also minimize the embedded files (for release). You can do this using the MSBuild task described in detail in this blog post .

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\ajaxmin.tasks" /> <PropertyGroup> <ResGenDependsOn> MinifyJavaScript; $(ResGenDependsOn) </ResGenDependsOn> </PropertyGroup> <PropertyGroup> <PostBuildEvent> </PostBuildEvent> </PropertyGroup> <Target Name="MinifyJavaScript" Condition=" '$(ConfigurationName)'=='Release' "> <Copy SourceFiles="@(EmbeddedResource)" DestinationFolder="$(IntermediateOutputPath)" Condition="'%(Extension)'=='.js'"> <Output TaskParameter="DestinationFiles" ItemName="EmbeddedJavaScriptResource" /> </Copy> <AjaxMin JSSourceFiles="@(EmbeddedJavaScriptResource)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".js" CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".css" JSLocalRenaming="CrunchAll" /> <ItemGroup> <EmbeddedResource Remove="@(EmbeddedResource)" Condition="'%(Extension)'=='.js'" /> <EmbeddedResource Include="@(EmbeddedJavaScriptResource)" /> <FileWrites Include="@(EmbeddedJavaScriptResource)" /> </ItemGroup> </Target> 
+3
source

There is an MVC framework available in MvcContrib to help manage reusable embedded resources. It is called Portable Areas . I used it before in an open source project called VoiceModel and it works great.

0
source

Source: https://habr.com/ru/post/1398224/


All Articles