Avatar of jackjohnson44
jackjohnson44
 asked on

asp.net mvc my route isn't working

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

ASP.NET.NET Programming

Avatar of undefined
Last Comment
Stephan

8/22/2022 - Mon
Mrunal

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.
Mrunal

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.
jackjohnson44

ASKER
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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Stephan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jackjohnson44

ASKER
All of my code is above.  I showed the Template controller and my single mapping.  Action comes from the map route.
Stephan

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.