Currently, I am facing difficulties in setting up the routing for my project. There are several cases that need to be handled, but I am struggling to make them work as intended.
case 1: / - Should route to the index of the angular app
Case 2: /{angular Route} - Should route to Angular's routing, but I believe it needs to be redirected to the angular base page first.
Case 3: /api/data/GetAllValues - Should route to the data API controller method and the GetAllValues action
The base file for Angular is stored at the root: /index.html
, and when you visit this page, Angular will take you to /Index
This is the routing setup I have in WebApiConfig.Register
, which is invoked by Global.asax
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
config.Routes.MapHttpRoute("AngularRedirect", "{.*}", "~/index.html");
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
And here are the Angular routes
.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/Index',
{
templateUrl: 'Templates/Feed.html',
controller: 'feedController'
}
);
$routeProvider.when('/Lookup',
{
templateUrl: 'Templates/Lookup.html',
controller: 'lookupController'
}
);
$locationProvider.html5Mode(true);
$routeProvider.otherwise({ redirectTo: '/Index' });
I am encountering some issues with this setup. Neither case 1 nor case 2 are working; both result in a message from ASP.NET stating that it cannot match a URL to the request.
Edit: This is the error message I receive when I try to navigate to / (case 1)
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:49569/'.","MessageDetail":"No route providing a controller name was found to match request URI 'http://localhost:49569/'"}
Update:
I modified the ASP.NET default route to catch {.*}, and now when I enter /
, it correctly redirects to Angular. However, when I input /Index
, I still encounter the same error as below
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:49569/Index'.","MessageDetail":"No route providing a controller name was found to match request URI 'http://localhost:49569/Index'"}