Solved

asp.net mvc my route isn't working

Posted on 2013-12-25
6
306 Views
Last Modified: 2014-01-04
I am having a very strange issue with my route in my global.asax.cs page.  

In the mapRoute below, if I use action = "Grid", everything works.  I can use either url below to navigate to the correct controller.  If I use action="Angular", both urls below go to the Angular Controller.  I have no idea why this is, but it took me a very long time to find.

These only work when the global file has action="Grid", otherwise they always go to Angular
http://localhost:53233/Template/Grid
http://localhost:53233/Template/Angular


    public class TemplateController : Controller
    {
        //
        // GET: /Template/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Grid()
        {
            return View("Grid");
        }

        public ActionResult Angular()
        {
            return View("Angular");
        }

Open in new window


routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Template", action = "Grid", id = UrlParameter.Optional }// Parameter defaults

Open in new window

0
Comment
Question by:jackjohnson44
  • 2
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Mrunal
Comment Utility
hi
Code you have given has build error.

Update with this:

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Template", action = "Grid", id = UrlParameter.Optional });// Parameter defaults

Open in new window


Comment out original one.
Create two views (Grid and Angular) inside
~\Views\Template\
path.

It is working for me.
0
 
LVL 22

Expert Comment

by:Mrunal
Comment Utility
simple way of creating view is:

inside Template controller, just right click on
View("Grid")
and select add view. It will take view name also automatically.

Same thing for Angular view also and RUN application.
0
 

Author Comment

by:jackjohnson44
Comment Utility
Hi, thanks.  The compile error was because I choppped the last line.  It should have included a ");".  My code looks right and should work, but something is causing the issue I talked about above.  Any ideas how to diagnose this?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Accepted Solution

by:
Stephan earned 500 total points
Comment Utility
This always helped me debug routes:
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx/

If I use action="Angular", both urls below go to the Angular Controller.

Do you have an angular controller or do you mean the method/action "Angular".
And where you do say action="Angular"? In a Html.ActionLink or something?

Maybe you could give us some more details in the result on how you are rendering this and what the results are. Maybe also add a screenshot of your project file with the controllers folder open.

Also, is there more than 1 route you specified or is this the only one?
0
 

Author Comment

by:jackjohnson44
Comment Utility
All of my code is above.  I showed the Template controller and my single mapping.  Action comes from the map route.
0
 
LVL 16

Expert Comment

by:Stephan
Comment Utility
I created a new MVC5 project with a templatecontroller (using your code) and the following mapRoute:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Template", action = "Angular", id = UrlParameter.Optional }
            );

Open in new window


On the code part it is pretty thesame of what you have sent us, only I don't have any issues.
Which version of MVC are you using and which Visual studio version?

Currently only thing that I can think of is that there is something interferring with the code you have besides the code you posted.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

6 Experts available now in Live!

Get 1:1 Help Now