eWorld.UI - Matt Hawley

Ramblings of Matt

RedirectToAction Nasty Bug in ASP.NET MVC Preview 3

June 2, 2008 22:04 by matthaw

We were converting the CodePlex application over to ASP.NET MVC Preview 3 today and found a nasty bug with RedirectToAction. In reality, the bug isn't so much around RedirectToAction, but a change they made internally to Routing. However, this seems to only happen in certain routing scenarios. Take the following routes:

   1:  routes.MapRoute("Login", "site/home/{action}",
   2:     new { controller = "session", action = "login" });
   3:  routes.MapRoute("User Info", "site/user/{action}",
   4:     new { controller = "user", action = "show" });

The problem crops up within your UserController actions when attempting to redirect to other UserController actions. For instance

   1:  public class UserController : Controller {
   2:     public ActionResult Show() { ... }
   3:     public ActionResult Create() {
   4:        return RedirectToAction("Show");
   5:     }
   6:  }

when line 4 is executed above, it attempts to redirect you to "~/site/home/foo". The reason is the change that was made is now trying to remove all the ambiguities by "assuming" things on the fly. Since the actionName overload of RedirectToAction doesn't take and doesn't supply the controller it cannot find the appropriate route. After a bit of convincing, the bug has been acknowledge but I make no guarantees if and when it'll be fixed in a future build - but in the mean time, you're stuck with

  1. Fixing the code yourself from the CodePlex source drop. This simply involves supplying the current executing controller's name in the route value dictionary.
  2. Use the RedirectToAction overload that takes both actionName and controllerName.
  3. Use my lambda expression based RedirectToAction which correctly sends both actionName and controllerName.

And, before anyone asks - why are you creating such complicated routing? Well, simple - simple routing equals a very simple application. Complex routing equals a pre-existing and well defined "RESTful based" urls that mean something when a user reads it.

kick it on DotNetKicks.com


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

June 5. 2008 06:07

Pingback from weblogs.asp.net

RedirectToAction Nasty Bug in ASP.NET MVC Preview 3 - eWorld.UI - Matt Hawley

weblogs.asp.net

June 5. 2008 07:13

I ran into the same problem - as this affects the URL generator too, a number of my forms were posting to the wrong url.

Jeremy Skinner

June 5. 2008 07:46

Forgot to mention the workaround that I used Smile

You can create a custom route that will process the RouteValueDictionary and then add it as the first route in your routetable:

RouteTable.Routes.Add(new RouteValuePreParser());

public class RouteValuePreParser : RouteBase {
public override RouteData GetRouteData(HttpContextBase httpContext) {
return null;
}

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
if(!values.ContainsKey("controller") && requestContext.RouteData.Values.ContainsKey("controller")) {
values.Add("controller", requestContext.RouteData.Values["controller"]);
}
return null;
}
}

Jeremy Skinner

June 5. 2008 12:31

I worked around it a different way too - all of my controllers inherit off of a BaseController and overrides the RedirectToAction method injecting in the controller name:

protected override RedirectToRouteResult RedirectToAction(string actionName, string controllerName, System.Web.Routing.RouteValueDictionary values)
{
if(String.IsNullOrEmpty(controllerName))
controllerName = RouteData.Values["controller"].ToString();

return base.RedirectToAction(actionName, controllerName, values);
}

Max Pool

July 17. 2008 22:19

The way I fixed was calling

return this.RedirectToRoute("TripFinder", new { controller = "TripFinder", action = "Index", id = id });

instead of the RederictToAction... plus u don't have to manipulate any code there fore making the upgrade straight forward once the new Preview comes out.

Samiq

June 1. 2009 04:55

thanks for sharing this.i have been looking for this

Indianapolis SEO

June 2. 2009 10:36

Great article

yazılım

June 26. 2009 02:19

thanks for the post

Life Experience Degrees

July 5. 2009 04:04

its a aweful bug but it has been rectified now

Irs problems

July 5. 2009 04:07

bug has been fixed in the new version.kindly download the new one

Meaning

July 8. 2009 17:16

Thanks for the routes around this bug. Great instructions. thanks

Indianapolis Website Design

July 8. 2009 17:20

Thanks for a great tutorial, very helpful with this bug. thanks.

Legal Marketing

July 10. 2009 04:17

Seems like a horrible bug, thanks for the work around!

Web Marketing

July 11. 2009 09:40

I hope this fixes everything. I was having trouble with redirecting.

Ways to Make Money

July 16. 2009 23:22

Valuable information for all.I will recommend my friends to read this for sure…

SEO

July 19. 2009 07:49

Thank you for useful information was a good article

sex movies

July 21. 2009 00:59

thanks you

Porno izle

July 26. 2009 07:15

@Samiq - that worked for me thanks

Bloomingdales Coupons

July 31. 2009 17:06

Hi,

Good post....thanks for sharing.. very useful for me i will bookmark this for my future needed. thanks for a great source.

seo software

August 2. 2009 17:58

hi all.
thank too the apple. http://www.germanporn.tk

Porno filme

August 2. 2009 18:00

Thanks a lot for this scripot.I have been looking for it for a long time http://www.tvsexizle.com

Porno izle

August 8. 2009 18:27

Awesome. just awesome...i haven't any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.i found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me.Really the blogging is spreading its wings rapidly. Your write up is fine example of it

casino online

August 9. 2009 20:02

Good post....thanks for sharing.. very useful for me i will bookmark this for my future needed. thanks for a great source.

tiffany

August 11. 2009 13:48

I also bookmarked it, thanks

web svetainiu kurimas

August 16. 2009 06:07

The script was very useful in figuring out what you are talking about. It's great that you gave this sample.

NJ Speed Dating

August 17. 2009 07:18

Nice find. Bookmarked your site. Keep up the good work.

Golf Products

August 24. 2009 23:01

This simply involves supplying the current executing controller's name in the route value dictionary.

Laptop Driver

August 26. 2009 05:48

Very good and interesting site Smile

no Deposit Casino bonus

September 2. 2009 06:05

How to encrypt the id at the time of passing from one page to another in asp.net+C# and decrypt also.plz help?

Internet Marketing

September 24. 2009 14:48

wonderful post

Canon t1i

September 25. 2009 04:07

Can u compare jsp and asp.net over security issues, fastness, cost effective? Which is best to avoid hacking?

asus laptops

September 26. 2009 20:27

Ugh, what a nasty bug, and great job finding it; this will only make the preview more powerful that you were able to fix it now. Good luck with continuing the studies.

Free Internet Advertising

October 1. 2009 07:29

Nice blog thanks for sharing

Cheap SEO

October 6. 2009 16:54

Seems like a horrible bug, thanks for the work around!

auto insurance

October 8. 2009 16:38

Generic Viagra Online Pharmacy. Buy Viagra Online and Order Cheap Viagra Prescription with Satisfaction Guarantee.

Buy Viagra

October 10. 2009 02:47

wonderful post

Sports scores

October 10. 2009 02:48

Nice blog thanks for sharing

soccer scores

October 10. 2009 02:48

Very good and interesting site Smile

buy tadalafil

October 10. 2009 02:48

This simply involves supplying the current executing controller's name in the route value dictionary.

cheap viagra

October 10. 2009 02:55

How can i run asp.net in internet ,intranet?

trading pins

October 10. 2009 06:19

Credit brokers are frequently ignored when looking for a mortgage. A lot of people do not appreciate the savings that can be made by using a consultant. when looking for a cheap finance quote. Not only can they assist you save you cash, they can in addition offer you important information, and help you realize the complexities that loans involve.

Mortgage Advisors

October 12. 2009 23:57

I love the post and I coming tomorrow to read more.

web design

October 15. 2009 17:37

nice post! thank you for sharing this useful info..

<a href="www.seattlecarpetcleaning.com">carpet cleaning</a>
<a href="www.heavensbest.com/.../...aning.html">rug cleaning</a>

peter north

October 15. 2009 21:20

You can create a custom route that will process the RouteValueDictionary and then add it as the first route in your routetable:

RouteTable.Routes.Add(new RouteValuePreParser());

public class RouteValuePreParser : RouteBase {
public override RouteData GetRouteData(HttpContextBase httpContext) {
return null;
}

games

October 16. 2009 01:00

I'm glad to know about your problem.Your problem very helpfull for me.

<a href="http://www.onlinemovieguru.com"> watch free movies</a>

cyber

October 17. 2009 05:16

thanks for sharing this.i have been looking for this

free online games

October 17. 2009 05:43

Credit brokers are frequently ignored when looking for a mortgage. A lot of people do not appreciate the savings that can be made by using a consultant. when looking for a cheap finance quote. Not only can they assist you save you cash, they can in addition offer you important information, and help you realize the complexities that loans involve.

antique chair

October 18. 2009 20:19

Arguable problem! but tables are not desk even antique ones

table

October 18. 2009 20:32

Not only can they assist you save you cash, they can in addition offer you important information, and help you realize the complexities that loans involve.

buffet

October 18. 2009 20:38

Really? Chest of drawer belongs to Victorian furniture

chest

October 18. 2009 21:06

Not only can they assist you with mirrors but with other antiques

mirror

October 18. 2009 22:00

Cupboard and sideboard are like antique brothers

cupboard

October 18. 2009 22:04

Can u compare jsp and asp.net over security issues

antique desks

October 18. 2009 22:24

the person who create this post it was a great human..

porcelain

October 19. 2009 10:51

When is the next post comming on this topic.


Regards

Lucu

sonoma hotels

October 19. 2009 14:48

Thanks for this, I have been looking for a fix to this nasty bug!

Sell and rent back

October 20. 2009 10:20

Good post....thanks for sharing.. very useful for me i will bookmark this for my future needed. thanks for a great source.

silver sterling

October 21. 2009 02:36

GOOD JOB,I like it!

ed hardy

October 21. 2009 03:35

You have been brilliant, keep impressing us.

Medela Symphony

October 21. 2009 03:59

Hi nice site design


Regards

Mainn

pet stroller

October 21. 2009 04:57

I posted your blog to my facebook group


Regards

Cooper

pet sitting fort worth

October 21. 2009 13:09

Indeed an interesting post i liked your blog. You are doing superb work, keep it up.

underground electric dog fence

October 21. 2009 13:16

I was facing the same problem.. Thanks for sharing this article.

<a href="http://www.thefilmwall.com/index/"> Watch Movies Online</a>

Mitsha

Comments are closed

Copyright © 2000 - 2010 , Excentrics World