Solved

Trying to get a route working in ASP.Net MVC

Posted on 2013-01-22
9
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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