当前位置:编程学习 > C#/ASP.NET >>

MVC在网页中引用js脚本,下面两种格式的效果是一样的吗?一样的有什么区别?

--------------------编程问答--------------------

        public string Content(string contentPath) {
            return Content(contentPath, RequestContext.HttpContext);
        }

        internal static string Content(string contentPath, HttpContextBase httpContext) {
            if (String.IsNullOrEmpty(contentPath)) {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "contentPath");
            }

            if (contentPath[0] == '~') {
                return PathHelpers.GenerateClientUrl(httpContext, contentPath);
            }
            else {
                return contentPath;
            }
        }
--------------------编程问答--------------------
     // this method can accept an app-relative path or an absolute path for contentPath
        public static string GenerateClientUrl(HttpContextBase httpContext, string contentPath) {
            if (String.IsNullOrEmpty(contentPath)) {
                return contentPath;
            }

            // many of the methods we call internally can't handle query strings properly, so just strip it out for
            // the time being
            string query;
            contentPath = StripQuery(contentPath, out query);

            return GenerateClientUrlInternal(httpContext, contentPath) + query;
        }

        private static string GenerateClientUrlInternal(HttpContextBase httpContext, string contentPath) {
            if (String.IsNullOrEmpty(contentPath)) {
                return contentPath;
            }

            // can't call VirtualPathUtility.IsAppRelative since it throws on some inputs
            bool isAppRelative = contentPath[0] == '~';
            if (isAppRelative) {
                string absoluteContentPath = VirtualPathUtility.ToAbsolute(contentPath, httpContext.Request.ApplicationPath);
                string modifiedAbsoluteContentPath = httpContext.Response.ApplyAppPathModifier(absoluteContentPath);
                return GenerateClientUrlInternal(httpContext, modifiedAbsoluteContentPath);
            }

            // we only want to manipulate the path if URL rewriting is active, else we risk breaking the generated URL
            NameValueCollection serverVars = httpContext.Request.ServerVariables;
            bool urlRewriterIsEnabled = (serverVars != null && serverVars[_urlRewriterServerVar] != null);
            if (!urlRewriterIsEnabled) {
                return contentPath;
            }

            // Since the rawUrl represents what the user sees in his browser, it is what we want to use as the base
            // of our absolute paths. For example, consider mysite.example.com/foo, which is internally
            // rewritten to content.example.com/mysite/foo. When we want to generate a link to ~/bar, we want to
            // base it from / instead of /foo, otherwise the user ends up seeing mysite.example.com/foo/bar,
            // which is incorrect.
            string relativeUrlToDestination = MakeRelative(httpContext.Request.Path, contentPath);
            string absoluteUrlToDestination = MakeAbsolute(httpContext.Request.RawUrl, relativeUrlToDestination);
            return absoluteUrlToDestination;
        }
--------------------编程问答-------------------- 直接写第二个得了  第一个得多做很多事吧 --------------------编程问答--------------------
引用 3 楼 zhuankeshumo 的回复:
直接写第二个得了  第一个得多做很多事吧

请问能说一下两者的区别吗 --------------------编程问答--------------------
引用 4 楼 whoamiwho 的回复:
Quote: 引用 3 楼 zhuankeshumo 的回复:

直接写第二个得了  第一个得多做很多事吧

请问能说一下两者的区别吗
UrlHelper.Content:将虚拟(相对)路径转换为应用程序绝对路径。 --------------------编程问答--------------------  MVC4/Razor2.0 里面用第二种就好了 微软的东西有时候就这样让人很蛋疼  新瓶装旧酒  --------------------编程问答-------------------- 要是网站放在虚拟目录下可能会有问题 --------------------编程问答-------------------- 第一种没问题,但是没有智能提示 哎 --------------------编程问答--------------------
引用 7 楼 zhang_zhen1234 的回复:
要是网站放在虚拟目录下可能会有问题

第二种使用的虚拟路径,会有什么问题呢? --------------------编程问答--------------------
引用 9 楼 whoamiwho 的回复:
Quote: 引用 7 楼 zhang_zhen1234 的回复:

要是网站放在虚拟目录下可能会有问题

第二种使用的虚拟路径,会有什么问题呢?

有时候你的网站放在二级目录下的时候 有可能找不到实际的js文件
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,