Solved

asp.net mvc my route isn't working

Posted on 2013-12-25
6
325 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
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 39739965
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
ID: 39739966
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
ID: 39741151
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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 16

Accepted Solution

by:
Stephan earned 500 total points
ID: 39741273
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
ID: 39741823
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
ID: 39741926
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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