处理请求,返回
public ActionResult Get() { //传递一个部分名称 var n = Request["n"]; n = n.Replace('/', '.'); //获取所有的程序集 var allTypes = AppDomain.CurrentDomain.GetAssemblies(); //查找含有相关资源名为(Gxu.Cisco.BLL)的程序集 var s = allTypes.First(f => f.FullName.StartsWith("Gxu.Cisco.BLL")); // var sname = s.GetManifestResourceNames(); //返回对应文件名 var ss = sname.First(f => f.EndsWith(n)); var d = s.GetManifestResourceStream(ss); //查找复合条件的程序集名称 //判断返回类型 if (ss.EndsWith("js")) { return File(d, "application/x-javascript"); } else if(ss.EndsWith("css")) { return File(d, "text/css"); } else if (ss.EndsWith("png")) { return File(d, "image/png"); } else if (ss.EndsWith("jpg")) { return File(d, "image/jpeg"); } return File(d, "*/*"); }
根据JS或者css调用 get()方法
/// <summary> /// 查找script /// </summary> /// <param name="urlHelper">url方法</param> /// <param name="path">路径</param> /// <returns></returns> public static MvcHtmlString IncludeScript(this UrlHelper urlHelper,string path) { var src= urlHelper.Action("Get","Resource",new{n=path}); src = "<script src=""+ src + ""></script>"; return new MvcHtmlString(src); } /// <summary> /// 查找css /// </summary> /// <param name="urlHelper">url方法</param> /// <param name="path">路径</param> /// <returns></returns> public static MvcHtmlString IncludeCss(this UrlHelper urlHelper, string path) { var src = urlHelper.Action("Get", "Resource", new { n = path }); src = "<link href="" + src +"" rel="stylesheet" />"; return new MvcHtmlString(src); }
资源更改
把css或者JS修改成嵌入的资源
css样式路径:因为上面的控制器是Resource 所以在css样式里修改路径为:url(/Resource/Get?n=/img/login-background.jpg)