asp.net mvc - AngularJS hash url and MVC routing -


i facing issue in navigating url. default page set login , application url http://localhost:12345/#/.

this works there 2 ways user can login application

  1. direct through application.
  2. getting username , password trough query string.

when application logging through query string url http://localhost:12345?auth=123654654656564/#/.

i remove auth value url. tried map routing doesn't work.

routes.maproute(             name: "default",             url: "{controller}/{action}",             defaults: new { controller = "account", action = "login"} ); 

and tried create 1 more action result return view

routes.maproute(             name: "actualdefault",             url: "{controller}/{action}",             defaults: new { controller = "account", action = "loginquery" } ); 

controller:

 public actionresult login()  {      if (request.querystring.count > 0 && request.querystring != null)      {          //validating          return redirecttoaction("loginquery", "account");      }      else      {          return view();      }  }  public actionresult loginquery()  {         return view("index");  } 

the above code removes query string url http://localhost:12345/account/loginquery/#/.

i need url http://localhost:12345/#/.

logging in via query string

i negligent not point out extremely bad practice. should use http post when logging application , send user secrets in body of post, not query string.

see

note can create forms in plain html (or via angularjs) call mvc action method, or can make http post via javascript or other programming language same thing.

query string values completely ignored mvc routing. can make custom route use query string values.

public class loginviaquerystringroute : routebase {     public override routedata getroutedata(httpcontextbase httpcontext)     {         var path = httpcontext.request.path;          if (!string.isnullorempty(path))         {             // don't handle urls have path /controller/action             return null;         }          var querystring = httpcontext.request.querystring;          if (!querystring.haskeys())         {             // don't handle route if there no query string.             return null;         }          if (!querystring.allkeys.contains("username") && !querystring.allkeys.contains("password"))         {             // don't handle case controller , action missing.             return null;         }          var routedata = new routedata(this, new mvcroutehandler());          routedata.values["controller"] = "account";         routedata.values["action"] = "loginquery";         routedata.values["username"] = querystring["username"];         routedata.values["password"] = querystring["password"];          return routedata;     }      public override virtualpathdata getvirtualpath(requestcontext requestcontext, routevaluedictionary values)     {         return null;     } } 

usage

public class routeconfig {     public static void registerroutes(routecollection routes)     {         routes.ignoreroute("{resource}.axd/{*pathinfo}");          routes.add(new loginviaquerystringroute());          routes.maproute(             name: "default",             url: "{controller}/{action}/{id}",             defaults: new { controller = "home", action = "index", id = urlparameter.optional }         );     } } 

this route match http://localhost:12345/?username=foo&password=bar , send loginquery action method.

logging in via http://localhost:12345/#/

it unclear how expect work. since after hash tag not sent server browser, http://localhost:12345/#/ equivalent http://localhost:12345/. so, saying "i want home page login page".

in typical mvc application, setup authorizeattribute on home page redirect user login page. after user logs in, redirected home page (or whatever secured page requested).

[authorize] public actionresult index() {     return view(); } 

if want all of application secured, can register authorizeattribute globally , use allowanonymousattribute on public action methods (such login , register pages).

public class filterconfig {     public static void registerglobalfilters(globalfiltercollection filters)     {         filters.add(new authorizeattribute());         filters.add(new handleerrorattribute());     } } 

and login action methods:

[allowanonymous] public actionresult login() {     //... }  [allowanonymous] [httppost] public actionresult login(loginmodel model) {     //... }  [allowanonymous] public actionresult loginquery(string username, string password) {     //... } 

but then, typical mvc-only application.

if using angular make spa, different story. namely, switch views on client side without doing http 302 redirect login form (perhaps popup - knows). point is, without details of how client setup communicate mvc, not possible give useful advice on setting mvc client beyond how typically setup mvc work in multi-page application.

note: can tell routing misconfigured. default , actualdefault definitions cannot exist in same route configuration because first match wins, therefore first 1 run , other 1 never run. both of route url definitions match any url 0, 1, or 2 segments in length, whichever have first in route table match , other 1 unreachable execution path.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -