简体   繁体   English

ASP.NET MVC中默认路由的Url.Content问题

[英]Problem with Url.Content in ASP.NET MVC on Default Route

If I use the following line in my default view /Home/Index 如果我在默认视图/ Home / Index中使用以下行

<script language="javascript" src="<%=Url.Content("~/Scripts/jquery-1.3.2.js")%>" type="text/javascript" ></script>

If I surf to this location using the following url http ://127.0.0.1:9999/Home/Index the page gets rendered correctly 如果我使用以下网址http://127.0.0.1:9999 / Home / Index浏览此位置,页面将正确呈现

<script language="javascript" src="/Scripts/jquery-1.3.2.js" type="text/javascript" ></script>

If I use the following url http ://127.0.0.1:9999/ (default wired to Home/Index) the page renders this: 如果我使用以下URL http://127.0.0.1:9999 /(默认连接到Home / Index)页面呈现如下:

<script language="javascript" src="//Scripts/jquery-1.3.2.js" type="text/javascript" ></script>

Does anyone has any idea how to solve this issue? 有谁知道如何解决这个问题?

EDIT: 编辑:

FYI: I'm using ASP.NET mvc 2 RC And this is my route configuration: 仅供参考:我正在使用ASP.NET mvc 2 RC这是我的路由配置:

routes.MapRoute(
 "Default",                                              // Route name
 "{controller}/{action}/{id}",                           // URL with parameters
 new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

If you use IIS6 or WinXP Cassini you should register one more route: 如果您使用IIS6或WinXP Cassini,您应该再注册一个路由:

if (Environment.OSVersion.Version.Major < 6) // IIS6 and WinXP Cassini
        {
            routes.MapRoute(
                "Root",
                "",
                new
                    {
                        controller = "Home",
                        action = "Index",
                        id = UrlParameter.Optional
                    }
                );
        }

Why are you keeping your id as an empty string? 为什么要将id保持为空字符串? I think this may be causing the problem. 我认为这可能会导致问题。 You may find better results by trying the following: 您可以通过尝试以下方法找到更好的结果:

routes.MapRoute(
 "Default",                                              // Route name
 "{controller}/{action}/{id}",                           // URL with parameters
 new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

to this 对此

routes.MapRoute(
 "Default",                                              // Route name
 "{controller}/{action}/{id}",                           // URL with parameters
 new { controller = "Home", action = "Index", UrlParameter.Optional }  // Parameter defaults
);

Well, alternatively you can do the following: 那么,您也可以执行以下操作:

create a key in appSettings section of web.config file. 在web.config文件的appSettings部分创建一个键。

<add key="DomainName" value="http://http://127.0.0.1:9999/" />

Now, whenever you want to assign "src" value of any image, javascript of css file, you can use this key. 现在,无论何时你想为任何图像分配“src”值,css文件的javascript,你都可以使用这个密钥。 it will be define a root for you and after that you can define at what path you have placed your file. 它将为您定义一个根,然后您可以定义放置文件的路径。 ie in your case: 即在你的情况下:

<script language="javascript" src="<%=System.Configuration.ConfigurationManager.AppSettings["DomainName"] %>Scripts/jquery-1.3.2.js" type="text/javascript" ></script>

I had a very similar problem with Asp.net using Request.ApplicationPath... and wrapped it as follows 我使用Request.ApplicationPath ...与Asp.net有一个非常类似的问题,并将其包装如下

    public string AppRoot()
    {
        var appPath = Request.ApplicationPath;
        if (appPath.EndsWith("/"))
            return appPath;
        else
            return appPath + "/";

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM