I'm reading this article and I still can't get around my head on how Angular's UI Router is working with ASP.Net routing.
Can someone explain in an easy and clear way, the full life cycle from when a URL is typed into the browser manually (ex: http://myapp/stateOne ). Who handles the request, ASP.Net? If not, how does angular intercept a URL request if the page is being served by ASP before it can step in? And when does AngularJS come in with the state provider?
I'm very confused on this and can't find much on the idea.
In addition to the question, I have a couple of other things I can't understand. I'm writing an app where:
Edit:
Special attention to this part, which uses black magic to cause routeTwo/6
to load the index page with the routeTwo page loaded in a UI-View if I'm understanding correctly.
HTML5 mode is working, but only in a very superficial way. A refresh of the page is sending the full URL to the server (as we have removed the scotch) which doesn't know what to do. We can fix this by reconfiguring MVC's RouteCollection properly. We need to be explicit about the route for each of our views, and then add a catch-all which sends all other URL's to our landing page, to be handled by Angular.
Update the RegisterRoutes method inside App_Start => RouteConfig.cs like so:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "routeOne",
url: "routesDemo/One",
defaults: new { controller = "RoutesDemo", action = "One" });
routes.MapRoute(
name: "routeTwo",
url: "routesDemo/Two/{donuts}",
defaults: new { controller = "RoutesDemo", action = "Two", donuts = UrlParameter.Optional });
routes.MapRoute(
name: "routeThree",
url: "routesDemo/Three",
defaults: new { controller = "RoutesDemo", action = "Three" });
routes.MapRoute(
name: "login",
url: "Account/Login",
defaults: new { controller = "Account", action = "Login" });
routes.MapRoute(
name: "register",
url: "Account/Register",
defaults: new { controller = "Account", action = "Register" });
routes.MapRoute(
name: "Default",
url: "{*url}",
defaults: new { controller = "Home", action = "Index" });
}
Happy Path Lifecycle
Client-Side Routing
Once your application has loaded, client-side routing modules like ui-router ngRoute or the new component router will control the route for you and load content that is bound to that route. However, this only works properly if you always maintain to a pre-defined root route that you use to bootstrap your Angular application.
I.E. www.site.com/Home#/login
or www.site.com/Home!/login
if using $locationProvider.hashPrefix('!').html5Mode(true);
.
Fully reloading your root URL will load your main index content, re-initialize the angular application and pick up the corresponding client-side routing (which in this case would be the login route and module associated with that route). However, once you deviate from this, you run into unhappy path concerns.
Unhappy Path
If you request a URL that does NOT return the entrance point to the AngularJS application, Angular has no way to intercept the request and, subsequently will never run. As a result, if a request is being made to a URL that is not the entrance point to the Angular application, you have to decide how you want to handle it. A common approach is to redirect to Index.
Additional Questions
What This Might Look Like
You would want to declare a route that handles your default entrance point:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "LoadMain", id = UrlParameter.Optional }
);
}
public class HomeController : Controller
{
public ActionResult LoadMain()
{
return View("Main.cshtml");
}
}
public class CategoriesController : Controller
{
public ActionResult Index()
{
return PartialView("Index.cshtml");
}
}
This way, if a user hit www.site.com
, we would return the content from /Home/LoadMain. This content would include all of our required Angular references (Angular, UI-Router, our main app, etc), which would then allow us to default our client-side route to /home and subsequently load /Home/Index
Main.cshtml:
<head>
<script src="//angular.js"></script>
<script src="//angular-ui-router.min.js"></script>
<script src="app.js"></script>
<body ng-app="app">
<div ui-view></div>
</body>
app.js
var app = angular.module('app', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/Categories/Index' // corresponds to an MVC partial route
})
.state('login', {
url: '/login',
templateUrl: '/Login/Index' // corresponds to an MVC partial route
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With