![](/img/trans.png)
[英]How to differentiate between / (the root) and /default.aspx in ASP.NET
[英]ASP.NET Root Url Doesn't Resolve to Default.aspx
我有一個asp.net網站,該網站最初加載時似乎無法解析為Default.aspx。 當我在本地計算機上調試時,它會加載Default沒問題。 除非我嘗試導航到“ localhost:##### /”。 然后,它會顯示404錯誤。 當我將其部署到登台服務器時,為其提供虛擬路徑“ mywebapp”,並從“ mydomain.com/mywebapp”加載它,同時提供404。 我已將Default.aspx設置為IIS中默認文檔的列表的頂部。 如果我導航到“ mydomain.com/mywebapp/default”,則該網站可以正常加載。 有什么建議么? 我會粘貼代碼,但這是一個大型網站,老實說,我不確定我在尋找什么。
編輯:在我的站點中,我還使用DataTables來顯示和編輯數據。 在ajax調用中,我以前可以使用以下URL來調用控制器:
api/MyController/idvalue
但是自從發現這一點以來,我不得不回過頭來為這些網址添加前綴以使它們正常工作:
mywebapp/api/MyController/idvalue
控制器:
public class MyController : ApiController
{
[Route("api/MyContoller/{idvalue}")]
[HttpGet]
[HttpPost]
public IHttpActionResult MyControllerMethod(intidvalue)
{
}
}
WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
RouteConfig:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { action = "Index", id = UrlParameter.Optional }
// );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
找到了解決方案。 在我的RouteConfig.cs中,如果我將以下行注釋掉:
controller = "Home",
它工作正常。
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { action = "Index", id = UrlParameter.Optional }
// );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
//controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
但是,我不確定為什么會這樣。 因此,如果有人能夠向我解釋這一點,那就太好了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.