Solved

Trying to get a route working in ASP.Net MVC

Posted on 2013-01-22
9
298 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

636 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