comment 0

How to create URL in ASP.Net MVC


This is probably a little old, but I am having some trouble so I figured I would start a post to see if I could help some people and possibly get some help myself. I have a Controller that is handling a shopping cart. The index method is pretty straight forward. Two parameters: the Cart it self and a returnURL as a string.

The returnURL if it is empty should have a “default” URL that it can use. (This is where the trouble came in.) In trying to be a good developer I added a test method to test this particular piece of code.

This is where the issue comes in. This test continually fails with a NullReferenceException. I originally thought it was that the returnUrl value was null. Which lead me to post a question on StackOverflow. (I will save that rant for another post.) Then as I was stepping through the debugger for the unit testing I discovered that the problem is not the returnURL being null, it is that the this.URL.Action object is actually null. (Which would not happen in a normal “live” or even a debugging environment. It is because I was running the test on that controller that caused the issue.

This brings me to the issue, how would one create a URL in an MVC controller as to avoid the NullReferenceException in the test, and still have the code work properly in production?

Leave a Reply

Your email address will not be published. Required fields are marked *