刷新页面时RouteConfig触发500错误

时间:2014-12-30 13:53:59

标签: asp.net asp.net-mvc angularjs

我在ASP.NET使用angularJS。当我运行我的Web应用程序时,所有链接都在工作,没有500错误。

但是当我刷新页面时,我得到了500个像这样的错误:

  

未找到部分视图“联系人”或视图引擎不支持搜索的位置。搜索了以下位置:

我正在使用angularJS控制器从./templates文件夹加载模板,而angularJS正在很好地完成这项工作......

有人知道我为什么会收到此错误以及如何解决此问题吗?

同样在View文件夹中只有Index.cshtml文件,因为我从./templates目录加载模板 这是RouteConfig.cs代码:

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 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);

});

1 个答案:

答案 0 :(得分:4)

该问题与未正确设置server方有关。首先尝试关闭html5模式

//$locationProvider.html5Mode(true);
$locationProvider.html5Mode({enabled: false});

并检查刷新后是否正常工作。这应该。

像这样更改路由:

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
    }
);

这应该做我们需要的。每当有任何东西进入服务器时:

  • 以js,html,css结尾......返回
  • /api/(ASP.NET Web API)也在此处跳过
  • 并且/somestuff/somepart之类的任何网址都被视为Home/Index

此设置url: "{dummyController}/{dummyAction}/{id}",进行了上述操作。来自html5mode的任何部分都被视为路由键“dummyController”,“dummyAction”,而Home和Index作为控制器和操作传递为默认值:new { controller = "Home", action = "Index" ...

因为您的应用程序需要路由的某些部分,所以您应该像这样使用它:

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
        }

);

同时检查此

How to: Configure your server to work with html5Mode