RouteConfig 刷新页面时触发 500 错误 [英] RouteConfig triggers 500 error when refreshing page
问题描述
我在 ASP.NET
中使用 angularJS.当我运行我的 Web 应用程序时,所有链接都在工作并且没有 500 错误.
I am using angularJS with ASP.NET
. When I run my Web application, all links are working and there is no 500 error.
但是当我刷新页面时,出现了这样的 500 错误:
But when I refresh page I got 500 error like this one:
未找到部分视图联系人"或没有视图引擎支持搜索的位置.搜索了以下位置:
The partial view 'Contacts' was not found or no view engine supports the searched locations. The following locations were searched:
我正在使用 angularJS 控制器从 ./templates 文件夹加载模板,而 angularJS 做得很好......
I am using angularJS controller to load template from ./templates folder and angularJS is doing that job very well...
有人知道我为什么会收到此错误以及如何解决吗?
Does someone knows why I am getting this error and how to fix it?
也在 View 文件夹中只有 Index.cshtml 文件,因为我从 ./templates 目录加载模板这是 RouteConfig.cs 代码:
Also inside View folder there is only Index.cshtml file because because I load templates from ./templates directory Here is RouteConfig.cs code:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("templates", "templates/{action}.html",
new { controller = "Home", action = "Templates", name = "" }
);
routes.MapRoute("contacts","contacts",
new { controller = "Home", action = "Contacts", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
控制器类:
public ActionResult Index()
{
return View();
}
// ActionResult prikazan ispod služi da bi angularJS
// mogao lodati HTML template u template folderu
public ActionResult Contacts()
{
return PartialView();
}
public ActionResult Templates()
{
return PartialView();
}
这里是 angularJS 路由配置:
and here is angularJS route config:
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/contacts',
{
controller: 'ContactsController',
templateUrl: 'templates/contacts.html'
})
.when('/add-contact',
{
controller: 'ContactAddController',
templateUrl: 'templates/addContact.html'
})
.when('/edit-contact/:contactId',
{
controller: 'ContactEditController',
templateUrl: 'templates/editContact.html'
})
.when('/display-contact/:contactId',
{
controller: 'ContactDetailsController',
templateUrl: 'templates/displayContact.html'
})
.when('/bookmarked-contacts',
{
controller: 'ContactsController',
templateUrl: 'templates/bookmarkedContacts.html'
})
.when('/search-contacts',
{
controller: 'SearchContactsController',
templateUrl: 'templates/searchContacts.html'
})
.otherwise({ redirectTo: '/contacts' });
$locationProvider.html5Mode(true);
});
推荐答案
该问题与server
端设置不当有关.首先尝试关闭html5
模式
The issue is related to improperly set server
side. Firstly try to turn off the html5
mode
//$locationProvider.html5Mode(true);
$locationProvider.html5Mode({enabled: false});
并检查刷新后是否一切正常.这应该.
And check that after refresh all is working. It should.
像这样更改路由:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("fonts*.woff");
routes.IgnoreRoute("*.js");
routes.IgnoreRoute("*.html");
routes.IgnoreRoute("*.css");
routes.IgnoreRoute("api/*");
routes.MapRoute(
name: "Default",
url: "{dummyController}/{dummyAction}/{dummy1}/{dummy2}/{dummy3}",
defaults: new { controller = "Home", action = "Index"
, dummyController = UrlParameter.Optional
, dummyAction = UrlParameter.Optional
, dummy1 = UrlParameter.Optional
, dummy2 = UrlParameter.Optional
, dummy3 = UrlParameter.Optional
}
);
这应该可以满足我们的需求.每当有任何东西进入服务器时:
This should do what we need. Whenever there is anything comming to server:
- 以js、html、css结尾……返回
/api/
(ASP.NET Web API) 此处也略过- 和任何像
/somestuff/somepart
这样的 url 都被视为Home/Index
- it ends with js, html, css ... it is returned
/api/
(ASP.NET Web API) is also skipped here- and any url like
/somestuff/somepart
is treated asHome/Index
此设置 url: "{dummyController}/{dummyAction}/{id}",
使上述内容.来自 html5mode 的任何部分都被视为路由键dummyController"、dummyAction",而 Home 和 Index 作为默认值中的控制器和动作传递: new { controller = "Home", action = "Index"...
This setting url: "{dummyController}/{dummyAction}/{id}",
makes the above. any part coming from html5mode is treated as a route key "dummyController", "dummyAction", while the Home and Index are passed as controller and action in the defaults: new { controller = "Home", action = "Index" ...
并且因为您的应用程序需要路由的某些部分,所以您应该像这样使用它:
And because your application is expecting some parts of your routing, you should use it like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("fonts*.woff");
routes.IgnoreRoute("*.js");
routes.IgnoreRoute("*.html");
routes.IgnoreRoute("*.css");
routes.IgnoreRoute("api/*");
// keep this application special settings
routes.MapRoute("templates", "templates/{action}.html",
new { controller = "Home", action = "Templates", name = "" }
);
routes.MapRoute("contacts","contacts",
new { controller = "Home", action = "Contacts", id = UrlParameter.Optional }
);
// this should do the job on F5
routes.MapRoute(
name: "Default",
url: "{dummyController}/{dummyAction}/{dummy1}/{dummy2}/{dummy3}",
defaults: new { controller = "Home", action = "Index"
, dummyController = UrlParameter.Optional
, dummyAction = UrlParameter.Optional
, dummy1 = UrlParameter.Optional
, dummy2 = UrlParameter.Optional
, dummy3 = UrlParameter.Optional
}
);
还要检查这个
这篇关于RouteConfig 刷新页面时触发 500 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!