Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to get a route working in ASP.Net MVC

Posted on 2013-01-22
9
Medium Priority
?
301 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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