Solved

Trying to get a route working in ASP.Net MVC

Posted on 2013-01-22
9
283 Views
Last Modified: 2013-02-15
Hi there

Hope you can help
I have a controller in called issuecompilationController where I have a route defined like this in global.asax.cs

routes.MapRoute(
"GetIssueComp",
"{controller}/{journalCode}/{volumeNumber}/{issueNumber}",
new { controller = "IssueCompilation", action = "GetIssueComp"}
);

And when I use this url http://localhost:4447/issuecompilation/ay/53/2

It fires this method in the controller no problem ....

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetIssueComp(string journalCode, int volumeNumber, int issueNumber)
{
  //some stuff
}


I also added this method in a new controller called issueController..

 [AcceptVerbs(HttpVerbs.Get)]
 public ActionResult GetIssue(string journalCode, int volumeNumber, int issueNumber)
 {
         return View();
 }
 and added this route..

routes.MapRoute(
  "GetIssue",
   "{controller}/{journalCode}/{volumeNumber}/{issueNumber}",
     new { controller = "Issue", action = "GetIssue"}
    );

But when I try this

And when I use this url http://localhost:4447/issue/ay/53/2

I get the error..
404 The resource cannot be found, it should fire the method like the other one.. it doesnt have to load a view.
0
Comment
Question by:LukePerrin
  • 5
  • 4
9 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38805424
I think you need to hard-code the controller name in for those routes:

            routes.MapRoute(
                "GetIssue",
                "Issue/{journalCode}/{volumeNumber}/{issueNumber}",
                new { controller = "Issue", action = "GetIssue" }
            );

            routes.MapRoute(
                "GetIssueComp",
                "IssueCompilation/{journalCode}/{volumeNumber}/{issueNumber}",
                new { controller = "IssueCompilation", action = "GetIssueComp" }
            );

Open in new window

0
 

Author Comment

by:LukePerrin
ID: 38805503
Thanks, but if I try that then what happens is when I call this
http://localhost:4447/issue/ay/53/2

it goes to the issueCompilation controller "GetIssueComp" method?!

Very weird...
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38805580
I'm not experiencing the same behavior. Do you have a default route set in your Routes.config? Also, what happens if you include the action in your URL when you test?

e.g.

http://localhost:4447/IssueCompilation/GetIssueComp/ay/53/2
http://localhost:4447/issue/GetIssue/ay/53/2
0
 

Author Comment

by:LukePerrin
ID: 38805603
If I specify the method name in the url I get the "resource cannot be found" error, where is the routes.config?

Thanks
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38805607
Under App_Start folder.
0
 

Author Comment

by:LukePerrin
ID: 38805635
I dont have that, what version of MVC are you using? I'm using MVC 2.

Thanks
0
 

Author Comment

by:LukePerrin
ID: 38805719
Another thing (sorry if your busy) is that if I try this
http://localhost:4447/shit/ddsds/32/32

It still calls the method on the issue compilation controller?! it seems to use that as its default. If I change the number of parameters on issue controller to distinguish it I still get the same problem of "resource not found" aarrgh!

Thanks
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38805823
Ah, then IIRC it should be within Global.asax (basically, wherever you added the new route). I believe the behavior you are seeing [even the "shitty" on  ; ) ] is because you have a default route.
0
 

Author Comment

by:LukePerrin
ID: 38805975
Ok I fixed it, I dont understand HOW I fixed so maybe you can shed some light on this?? I changed the issuecontroller method to accept another parameter and updated the route to take another parameter and that WORKED! so I called it like this
http://localhost:4447/issue/ay/53/2/2   (note extra parameter)

then I tried  
http://localhost:4447/bollocks/ay/53/2/2   and that gave resource not found, so this time no default (i havnt been changing defaults)

Meanwhile http://localhost:4447/issuecompilation/ay/53/2/2 still functioned as normal

I then changed issue controller back to 3 parameters and its route which is how I want it, I want it to take the same amount of paramterers as issue compilation and it still worked! but thats how I had it before??! so why didnt it work before.

Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now