简体   繁体   中英

Angular routing in ASP.NET MVC application

I'm trying to read query parameters in my angular controller so I set up the route config confirmedemailcontroller.js

var app = angular.module('confirmedemail', ['ngRoute']);
app.config(function ($routeProvider, $locationProvider) {
    // calls
    $routeProvider.when('/confirmedemail/:userid/:token', {
        templateUrl: 'templates/confirmedview.html',
                    controller: 'ConfirmedEmailController'
                });
    $locationProvider.html5Mode(true);
});

app.controller('ConfirmedEmailController', function ($scope, $http, $routeParams, $location) {
    // never calls
    // read params from $routeParams and send them then display the result...
    //...
});

ConfirmedEmail.cshtml

@{
    ViewBag.Title = "Confirm Email";
}

    <h2>@ViewBag.Title.</h2>
    <div data-ng-app="confirmedemail">
        <p>
            Thank you for confirming your email. Please @Html.ActionLink("Click here to Log in", "Login", "Home", routeValues: null, htmlAttributes: new { id = "loginLink" })
        </p>
        <div ng-view></div>
    </div>

my root is http://localhost:25715/confirmedemail/111/222 , where 111 is userId and 222 is token which I want to read out inside controller

But controller never calls. What have I missed?

Updated I have HomeController with working action

public ActionResult ConfirmedEmail(string userId = "", string code = "")
{
    ViewBag.Message = "Account confirmed";
    return View();
}

and in templates folder there's confirmedview.html

In RouteConfig.cs

  routes.MapRoute(
         name: "Confirm",
         url: "ConfirmedEmail/{userId}/{code}",
         defaults: new { controller = "Home", action = "ConfirmedEmail", userId = "", code = "" }
     );

in confirmedemailcontroller.js

var app = angular.module('confirmedemail', ['ngRoute']);
app.config(function ($routeProvider, $locationProvider) {
    $routeProvider.when('/ConfirmedEmail/:param1/:param2', {
        templateUrl: '/templates/confirmedview.html',
                    controller: 'ConfirmedEmailController'
                });
    $locationProvider.html5Mode({ requireBase: false, enabled: true });
});
app.controller('ConfirmedEmailController', function ($scope, $http, $routeParams) { ... });

And don't forget about case sensivity! If I call it like mysite.com/confirmedemail/111/222 , it won't work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM