Link to home
Start Free TrialLog in
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

Avatar of Mrunal
Mrunal
Flag of India image

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.
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.
Avatar of jackjohnson44
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?
ASKER CERTIFIED SOLUTION
Avatar of Stephan
Stephan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All of my code is above.  I showed the Template controller and my single mapping.  Action comes from the map route.
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.