开发者

Can I do this in ASP.NET MVC routing?

开发者 https://www.devze.com 2022-12-17 05:24 出处:网络
I\'m trying to do a site like http://example.com which goes to my SignIn action located at the Account controller.After signing in, I want to redirect to something like http://example.com/id where id

I'm trying to do a site like http://example.com which goes to my SignIn action located at the Account controller. After signing in, I want to redirect to something like http://example.com/id where id is the user's ID.

I already have this code in my Global.asax.cs.

 routes.MapRoute("Login2",
           "{bg}/{controller}/{action}",
          new { controller = "Account", action = "SignIn", bg = "" }
          );
 routes.MapRoute("Login",
            "{controller}/{action}",
           new { controller = "Account", action = "SignIn" }
           );
routes.MapRoute("Default",
           "{bg}/{controller}/{action}",
           new { controller = "Home", action = "Index", bg = "" }
      开发者_开发百科     ); 

When I go to http://example.com/id and the user is not yet authenticated, the sign-in form is displayed and it goes to the page I want it to redirect after signing in. In the case of just going to http://example.com, I'm doing some server-side validation to know the user's id and it also does it job.

When I'm in http://example.com/id and I want to sign-up, there's a link which goes to http://example.com/id/account/signup. It actually goes to the sign-up page. But when I go to http://example.com and I tried to hover the sign-up link, the link becomes http://account/signup instead of http://example.com/account/signup.


Check this out ScottGu's explanation on routing. its should help you. check it here


Yes you definitely can, I do the same thing on a blog site I created where I have routes like {blogSubFolder}/{controller}/{action}. The only comment I'd make is I'm not sure if the order of the defaults matter.

In my case I had them follow the same pattern as the route itself. So I'd say move the language = "" to be the first default rather than the last.

Sorry for the edit, half of my answer got cut off for some reason. Here is the remainder.

I actually tried that same route in a test app, and had some problems with it. If I just do the default route that MVC creates I can just specify the /Controller without any action at the end, and it all works fine. When I do the ssame thing with a preceding variable (such as your {language} or my {blogSubFolder}), it actually doesn't work. The is exactly the behavior that you are seeing. The only thing I can figure out from all of this is that if you don't have the controller first, then it appears that you must have the action specified in your url. I tried it with different combinations of including the default controller, and not including it, and also tried a number of different routes all without any success.

0

精彩评论

暂无评论...
验证码 换一张
取 消