RouteConfig触发500错误刷新页面时 [英] RouteConfig triggers 500 error when refreshing page
问题描述
我使用与 ASP.NET
angularJS。当我运行我的Web应用程序,各个环节都工作,没有500错误。
但是,当我刷新页面我得到500错误像这样的:
局部视图联系人没有被发现或没有视图引擎支持搜索位置。在以下地点进行了全面搜查:
块引用>我使用angularJS控制器从./templates文件夹中加载模板,angularJS就是干这个工作非常好...
请问有人知道为什么我收到此错误,以及如何解决它?
还可以查看文件夹里面只有Index.cshtml因为文件,因为我加载模板从./templates目录
这里是RouteConfig.cs code:公共类RouteConfig
{
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(模板,模板/ {}行动的.html
新{控制器=家,行动=模板,名字=}
);
routes.MapRoute(联系人,人脉,
新{控制器=家,行动=联系人,ID = UrlParameter.Optional}
);
routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
}
}Controller类:
公众的ActionResult指数()
{
返回查看();
}//的ActionResult prikazan ispodsluži达双angularJS
//莫高窟lodati HTML模板ù模板folderu
公众的ActionResult联系人()
{
返回PartialView();
}公众的ActionResult模板()
{
返回PartialView();}和这里是angularJS航线配置:
的app.config(函数($ routeProvider,$ locationProvider){
$ routeProvider
。当('/联系人',
{
控制器:'ContactsController',
templateUrl:'模板/ contacts.html
})
。当('/加接触,
{
控制器:'ContactAddController',
templateUrl:'模板/ addContact.html
})
。当('/编辑接触式/:的ContactID,
{
控制器:'ContactEditController',
templateUrl:'模板/ editContact.html
})
。当('/显示触点/:的ContactID,
{
控制器:'ContactDetailsController',
templateUrl:'模板/ displayContact.html
}) 。当('/书签,联系人的,
{
控制器:'ContactsController',
templateUrl:'模板/ bookmarkedContacts.html
})
。当('/搜索联系人的,
{
控制器:'SearchContactsController',
templateUrl:'模板/ searchContacts.html
})
不然的话({redirectTo:'/触点'});
$ locationProvider.html5Mode(真);});
解决方案的问题与设置不正确
服务器
的一面。首先尝试关闭HTML5
模式// $ locationProvider.html5Mode(真);
$ locationProvider.html5Mode({启用:假});和检查刷新后的所有工作。这应该。
更改路由是这样的:
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);
routes.IgnoreRoute(字体* .woff);
routes.IgnoreRoute(* js的。);
routes.IgnoreRoute(* HTML);
routes.IgnoreRoute(*的CSS。);
routes.IgnoreRoute(API / *);routes.MapRoute(
名称:默认,
网址:{dummyController} / {dummyAction} / {dummy1} / {dummy2} / {} dummy3
默认:新{控制器=家,行动=索引
,dummyController = UrlParameter.Optional
,dummyAction = UrlParameter.Optional
,dummy1 = UrlParameter.Optional
,dummy2 = UrlParameter.Optional
,dummy3 = UrlParameter.Optional
}
);这应该做的事情,我们需要的。每当有正在添加任何服务器:
- 将其与JS,HTML,CSS结束......它返回
/ API /
(的ASP.NET Web API)也跳过此处- 而像
/ somestuff / somepart
任何URL被视为
此设置
URL:{dummyController} / {} dummyAction /(编号),
使得上方。从html5mode未来任何部分被视为一个途径键dummyController,dummyAction,而家庭和索引作为控制器和动作的默认传递:新{控制器=家,行动= 索引...
和,因为你的应用程序期待您的路由的某些部分,你应该使用这样的:
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);
routes.IgnoreRoute(字体* .woff);
routes.IgnoreRoute(* js的。);
routes.IgnoreRoute(* HTML);
routes.IgnoreRoute(*的CSS。);
routes.IgnoreRoute(API / *); //保持这个应用程序的特殊设置
routes.MapRoute(模板,模板/ {}行动的.html
新{控制器=家,行动=模板,名字=}
);
routes.MapRoute(联系人,人脉,
新{控制器=家,行动=联系人,ID = UrlParameter.Optional}
); //这个应该做F5工作
routes.MapRoute(
名称:默认,
网址:{dummyController} / {dummyAction} / {dummy1} / {dummy2} / {} dummy3
默认:新{控制器=家,行动=索引
,dummyController = UrlParameter.Optional
,dummyAction = UrlParameter.Optional
,dummy1 = UrlParameter.Optional
,dummy2 = UrlParameter.Optional
,dummy3 = UrlParameter.Optional
});另外,请查阅本
如何配置你的服务器html5Mode 工作
I am using angularJS with
ASP.NET
. When I run my Web application, all links are working and there is no 500 error.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:
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?
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 } ); } }
Controller class:
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(); }
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); });
解决方案The issue is related to improperly set
server
side. Firstly try to turn off thehtml5
mode//$locationProvider.html5Mode(true); $locationProvider.html5Mode({enabled: false});
And check that after refresh all is working. It should.
Change the routing like this:
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:
- 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
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 } );
Also check this
How to: Configure your server to work with html5Mode
这篇关于RouteConfig触发500错误刷新页面时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!