MVC Cannot use a leading .. to exit above the top

2019-07-13 11:06发布

问题:

Have a Website build with ASP.NET MVC 3.

Publish it as below: create a virtual directory under website, copy MVC application to this folder and turn it to application. So the site structure looks like

Have UrlRewite as below so that http://www.example.com will be rewrite to http://www.example.com/travel without url changed.

<rule name="rewrite rule for home page" enabled="true" stopProcessing="false">
 <match url="^$" />
 <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
 <action type="Rewrite" url="/home/index" appendQueryString="false" />
</rule>
<rule name="rewrite rule for virtual path" enabled="true">
 <match url="^[\w-/\.]+$" />
 <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
 <action type="Rewrite" url="/travel/{R:0}" appendQueryString="false" />
</rule>

Then My problem came out.

script src="@Url.Content("~/Content/Js/Index.js")" type="text/javascript">

Url.RouteUrl("Lvxingshe")

Url.RouteUrl("ListOfCity", new { locationname = "LA" })

script src="@Url.Content("/Content/Js/Index.js")" type="text/javascript">

I have codes above in my home/index view. And they threw exceptions:

Cannot use a leading .. to exit above the top directory.

回答1:

In my case creating extra rule in route config solved problem.

Got this rule:

<rule name="site/company/CompanyId-CompanyName/" stopProcessing="true">
    <match url="^company/([0-9]+)-([^/^&amp;]+)(/{0,1})$" />
    <action type="Rewrite" url="employer/{R:1}" />
</rule>

In action url propery you should use this extra rule.

// for urlRewrite
routes.MapRoute("Employer", 
    "employer/{empid}", 
    new { controller = "Employer", action = "Index" }, 
    new { empid = @"\d+" }
);