MVC剃刀,包括从其他项目JS / CSS文件 [英] MVC Razor, Include JS / CSS files from another project
问题描述
我有一个使用剃刀语法的C#MVC项目。结果
为了能够重用一些code,我想把我的一些JavaScript和CSS文件在不同的项目,包括他们弄好了。结果
这是我的脚本是如何被包括ATM:
<脚本的src =@ Url.Content(〜/脚本/引导-typeahead.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/引导-dropdown.js)TYPE =文/ JavaScript的>< / SCRIPT>
在momemnt,脚本是在同一个项目的CSHTML文件。但是,脚本应该放在Common.Web项目,而不是...结果
我想要做的就是这个(不工作寿):
<脚本的src =@ Url.Content(Common.Web /脚本/引导-typeahead.js)TYPE =文/ JavaScript的>< /脚本>
<脚本的src =@ Url.Content(Common.Web /脚本/引导-dropdown.js)TYPE =文/ JavaScript的>< / SCRIPT>
我做的这事。不过,我嵌入JavaScript文件,并在另一个DLL等内容,然后叫他们从我的剃刀语法像这样。这里是code我用。
在查看:
脚本示例:
<脚本src=@Url.Action(GetEmbeddedResource,共享,新的{资源名称=Namespace.Scripts.jquery.qtip.min.js,pluginAssemblyName = @ Url.Content(〜/斌/ Namespace.dll)})类型=文/ JavaScript的>< / SCRIPT>
图片示例:
@ Html.EmbeddedImage(corporate.gif,新的{宽度= 150,高度= 50})
下面是我的辅助方法:
公共静态MvcHtmlString EmbeddedImage(此的HtmlHelper的HtmlHelper,串imageName,动态htmlAttributes)
{
UrlHelper URL =新UrlHelper(HttpContext.Current.Request.RequestContext);
变种锚=新TagBuilder(IMG);
anchor.Attributes [SRC] = url.Action(GetEmbeddedResource,共享,
新
{
资源名称=Namespace.Content.Images。 + imageName,
pluginAssemblyName = url.Content(〜/斌/ Namespace.dll)
}); 如果(htmlAttributes!= NULL)
{
串宽度=;
弦高=;
PI的PropertyInfo = htmlAttributes.GetType()的getProperty(宽度);
如果(PI!= NULL)
宽度= pi.GetValue(htmlAttributes,空)的ToString(); PI = htmlAttributes.GetType()的getProperty(身高)。
如果(PI!= NULL)
身高= pi.GetValue(htmlAttributes,空)的ToString(); 如果(!string.IsNullOrEmpty(高度))
anchor.Attributes [高度] =高度; 如果(!string.IsNullOrEmpty(宽度))
anchor.Attributes [宽度] =宽度;
}
返回MvcHtmlString.Create(anchor.ToString());
}
最后我的共享控制器:
[HTTPGET]
公共FileStreamResult GetEmbeddedResource(字符串pluginAssemblyName,字符串资源名称)
{
尝试
{
字符串physicalPath =使用Server.Mappath(pluginAssemblyName);
流流= ResourceHelper.GetEmbeddedResource(physicalPath,资源名称);
返回新FileStreamResult(流GetMediaType(资源名称));
//返回新FileStreamResult(流GetMediaType(TEM presourceName));
}
赶上(例外)
{
返回新FileStreamResult(新的MemoryStream(),GetMediaType(资源名称));
}
} 私人字符串GetMediaType(字符串FILEID)
{
如果(fileId.EndsWith(JS))
{
回报的文/ JavaScript的;
}
否则如果(fileId.EndsWith(CSS))
{
回报的文/ CSS
}
否则如果(fileId.EndsWith(JPG))
{
返回到图像/ JPEG;
}
否则,如果(fileId.EndsWith(GIF))
{
返回到图像/ GIF;
}
否则如果(fileId.EndsWith(PNG))
{
返回到图像/ PNG
}
返回文本;
}
资源助手:
公共静态类ResourceHelper
{
公共静态流GetEmbeddedResource(字符串physicalPath,字符串资源名称)
{
尝试
{
装配装配= PluginHelper.LoadPluginByPathName<大会>(physicalPath); 如果(组装!= NULL)
{
透射电镜字符串presourceName = assembly.GetManifestResourceNames()了ToList()FirstOrDefault(F => f.EndsWith(资源名称))。;
如果(TEM presourceName == NULL)
返回null;
返回assembly.GetManifestResourceStream(TEM presourceName);
}
}
赶上(例外)
{ } 返回null;
}
}
插件助手
公共静态牛逼LoadPluginByPathName< T>(字符串将pathName)
{
字符串viewType = typeof运算(T).GUID.ToString(); 如果(HttpRuntime.Cache [viewType]!= NULL)
返回HttpRuntime.Cache [viewType]为T? (T)HttpRuntime.Cache [viewType]:默认(T); 对象插件= Assembly.LoadFrom(将pathName);
如果(插件!= NULL)
{
//缓存,我们希望本次大会只加载到内存中,一旦此对象。
HttpRuntime.Cache.Insert(viewType,插件);
返回(T)的插件;
} 返回默认(T);
}
请记住,我使用这些嵌入式内容!
I've got a C# MVC project that uses Razor syntax.
To be able to reuse some code, I want to put some of my JavaScript and CSS files in a different project and include them somehow.
This is how my scripts are included atm:
<script src="@Url.Content("~/Scripts/bootstrap-typeahead.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/bootstrap-dropdown.js")" type="text/javascript"></script>
At the momemnt, the scripts are in the same project as the cshtml file. But the scripts should be placed in the Common.Web project instead...
What I want to do is this (doesn't work tho):
<script src="@Url.Content("Common.Web/Scripts/bootstrap-typeahead.js")" type="text/javascript"></script>
<script src="@Url.Content("Common.Web/Scripts/bootstrap-dropdown.js")" type="text/javascript"></script>
I do this very thing. However I embed the Javascript files and other content in another DLL and then call them from my razor syntax like so. Here is the code I use. In the View: Script example:
<script src=@Url.Action("GetEmbeddedResource", "Shared", new { resourceName = "Namespace.Scripts.jquery.qtip.min.js", pluginAssemblyName = @Url.Content("~/bin/Namespace.dll") }) type="text/javascript" ></script>
Image Example:
@Html.EmbeddedImage("corporate.gif", new { width = 150, height = 50})
Here is my helper methods:
public static MvcHtmlString EmbeddedImage(this HtmlHelper htmlHelper, string imageName, dynamic htmlAttributes)
{
UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
var anchor = new TagBuilder("img");
anchor.Attributes["src"] = url.Action("GetEmbeddedResource", "Shared",
new
{
resourceName = "Namespace.Content.Images." + imageName,
pluginAssemblyName = url.Content("~/bin/Namespace.dll")
});
if (htmlAttributes != null)
{
string width = "";
string height = "";
PropertyInfo pi = htmlAttributes.GetType().GetProperty("width");
if (pi != null)
width = pi.GetValue(htmlAttributes, null).ToString();
pi = htmlAttributes.GetType().GetProperty("height");
if (pi != null)
height = pi.GetValue(htmlAttributes, null).ToString();
if (!string.IsNullOrEmpty(height))
anchor.Attributes["height"] = height;
if (!string.IsNullOrEmpty(width))
anchor.Attributes["width"] = width;
}
return MvcHtmlString.Create(anchor.ToString());
}
Lastly my shared Controller:
[HttpGet]
public FileStreamResult GetEmbeddedResource(string pluginAssemblyName, string resourceName)
{
try
{
string physicalPath = Server.MapPath(pluginAssemblyName);
Stream stream = ResourceHelper.GetEmbeddedResource(physicalPath, resourceName);
return new FileStreamResult(stream, GetMediaType(resourceName));
//return new FileStreamResult(stream, GetMediaType(tempResourceName));
}
catch (Exception)
{
return new FileStreamResult(new MemoryStream(), GetMediaType(resourceName));
}
}
private string GetMediaType(string fileId)
{
if (fileId.EndsWith(".js"))
{
return "text/javascript";
}
else if (fileId.EndsWith(".css"))
{
return "text/css";
}
else if (fileId.EndsWith(".jpg"))
{
return "image/jpeg";
}
else if (fileId.EndsWith(".gif"))
{
return "image/gif";
}
else if (fileId.EndsWith(".png"))
{
return "image/png";
}
return "text";
}
Resource Helper:
public static class ResourceHelper
{
public static Stream GetEmbeddedResource(string physicalPath, string resourceName)
{
try
{
Assembly assembly = PluginHelper.LoadPluginByPathName<Assembly>(physicalPath);
if (assembly != null)
{
string tempResourceName = assembly.GetManifestResourceNames().ToList().FirstOrDefault(f => f.EndsWith(resourceName));
if (tempResourceName == null)
return null;
return assembly.GetManifestResourceStream(tempResourceName);
}
}
catch (Exception)
{
}
return null;
}
}
Plugin Helper
public static T LoadPluginByPathName<T>(string pathName)
{
string viewType = typeof(T).GUID.ToString();
if (HttpRuntime.Cache[viewType] != null)
return HttpRuntime.Cache[viewType] is T ? (T)HttpRuntime.Cache[viewType] : default(T);
object plugin = Assembly.LoadFrom(pathName);
if (plugin != null)
{
//Cache this object as we want to only load this assembly into memory once.
HttpRuntime.Cache.Insert(viewType, plugin);
return (T)plugin;
}
return default(T);
}
Remember that I am using these as embedded content!
这篇关于MVC剃刀,包括从其他项目JS / CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!