Solved

Trying to get a route working in ASP.Net MVC

Posted on 2013-01-22
9
291 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 75

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 75

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 
LVL 75

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 75

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

813 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