Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

asp.net - RouteConfig triggers 500 error when refreshing page

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

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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 as Home/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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...