Mar*_*tin 17 javascript c# asp.net-mvc razor
我有一个使用Razor语法的C#MVC项目.
为了能够重用一些代码,我想将一些JavaScript和CSS文件放在一个不同的项目中并以某种方式包含它们.
这就是我的脚本目前的包含方式:
<script src="@Url.Content("~/Scripts/bootstrap-typeahead.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/bootstrap-dropdown.js")" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
目前,这些脚本与cshtml文件位于同一个项目中,但它们应该放在Common.Web项目中......而
我想要做的就是这个(虽然不起作用):
<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>
Run Code Online (Sandbox Code Playgroud)
ret*_*lig 13
我做这件事.但是我将Javascript文件和其他内容嵌入到另一个DLL中,然后从我的剃刀语法中调用它们.这是我使用的代码.在View:Script示例中:
<script src=@Url.Action("GetEmbeddedResource", "Shared", new { resourceName = "Namespace.Scripts.jquery.qtip.min.js", pluginAssemblyName = @Url.Content("~/bin/Namespace.dll") }) type="text/javascript" ></script>
Run Code Online (Sandbox Code Playgroud)
图片示例:
@Html.EmbeddedImage("corporate.gif", new { width = 150, height = 50})
Run Code Online (Sandbox Code Playgroud)
这是我的帮手方法:
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());
}
Run Code Online (Sandbox Code Playgroud)
最后我的共享控制器:
[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";
}
Run Code Online (Sandbox Code Playgroud)
资源助手:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
插件助手
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);
}
Run Code Online (Sandbox Code Playgroud)
请记住,我使用这些作为嵌入内容!
据我所知,你不能这样做,因为路径不在网站之外.
但是,您可以执行以下操作:
1)将您想要共享的所有脚本放在Common.Web\Scripts中
2)对于Web应用程序中的每个脚本文件"Add as Link"到您的Common.Web脚本(您甚至不需要执行此步骤;它然而,很高兴看到您的Web应用程序在VS中使用了哪些脚本.)
3)将后期构建事件添加到Web应用程序,该事件将脚本从Common.Web\Scripts复制到WebApp\Scripts文件夹:
复制$(ProjectDir)..\Common.Web\Scripts*$(ProjectDir)\ Scripts
因此,从您在Visual Studio中的角度来看,您将只有一个位置来更新可供多个项目使用的.js文件.
| 归档时间: |
|
| 查看次数: |
12418 次 |
| 最近记录: |