The mvc controller action looks like this:
The code below goes into the Home controller, ContentFile action method, which
returns a FileStreamResult below.
public FileStreamResult ContentFile(string id)
{
string resourceName = Assembly.GetExecutingAssembly().GetManifestResourceNames().ToList().FirstOrDefault(f => f.EndsWith(id));
return new FileStreamResult(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName), GetMIMEType(id));
}
private string GetMIMEType(string fileId)
{
if (fileId.EndsWith(".js"))
{
return "text/javascript";
}
else if (fileId.EndsWith(".css"))
{
return "text/stylesheet";
}
else if (fileId.EndsWith(".jpg"))
{
return "image/jpeg";
}
return "text";
}
To use this in the views, write this:
<script type="text/javascript" src="/Home/ContentFile/test.js">
</script>
<link rel="Stylesheet" href="/Home/ContentFile/blue.css">
<img src="/Home/ContentFile/forest.jpg" />
To load up an embedded resource we look into the executing dll, which instead should perhaps load a specified assembly if necessary, and then GetManifestResourceStream and retrieving the matching resource name passed in. The MIME type must be set and is resolved by looking at the resource name passed in.
So the morale is that if you want to load a content file from a DLL in a MVC view, use a controller action and return a FileStreamResult and then load up the content file by writing the route to the ContentFile action.
This comment has been removed by the author.
ReplyDeleteUpdating my blog article using my Samsung Galaxy phone. The last part discusses content files. Yes, javascript, images and css is "content" - but in this context they are embedded resources.Updating my blog article using my Samsung Galaxy phone. The last part discusses content files. Yes, javascript, images and css is "content" - but in this context they are embedded resources.
ReplyDeleteumm that last comment got repeated twice. note in a deployment scenario there are issues with reflection being turned off. Anyways I mean this approach should work. An alternative is an Url extension method to a controller action. My code dont have exception handling. You probably should add that. Also a locking should be done when returning the stream to avoid thread issues of multiple requests.
ReplyDeleteAfter reading some more about MIME type, MIME is now considered the wrong term. Content Type or Internet media type is a better term than MIME, which now is used for email, as it stands for Multipurpose Internet Mail Extensions really ..
ReplyDeleteHi,
ReplyDeleteThank you for the article.
I've tried to apply your approach to my case, but it doesn't work immediately. Please, take a look at the issue here:
https://stackoverflow.com/questions/45370481/the-issue-with-access-to-js-file-from-external-assembly-in-mvc-5/45410502
What I'm doing wrong and how to make it work properly?