user3370768
user3370768

Reputation: 11

mvc 4 - how can i set the route settings to be correct and not needing to fix it in the browser?

this is how my route looks like: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

my controller name is "defaultPageController" and my view name is "defaultPage".

but when i lounch the page this is the url i get:

    http://localhost:47983/Views/DefaultPage/DefaultPage.cshtml

and then i need to delete the "/view/" and the ".cshtml" from the url and then it works fine.

why is this happening?

how can i change it?

tnx

Upvotes: 0

Views: 79

Answers (2)

user3370768
user3370768

Reputation: 11

thank you for your answers, i found what i did wrong.

the problem was that i set the view page as start up page and then the project just tried to skip the contoller.

i just right click on the project, then properties and then "web", there i decided the default url and route that i want to start with.

Upvotes: 0

Izekid
Izekid

Reputation: 185

That is the thing with MVC(Models Views Controllers) It will automaticly look in your view folder as your are instructing it to look for the folder in the views with the name Default. But if you do not want to use it you can of course change the route.

using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
    public class YourApplication : System.Web.YourApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "YourName",                                           // Route name
                "Folder/{entryDate}",                            // URL with parameters
                new { controller = "Archive", action = "Entry" }  // Parameter defaults
            );
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
        }
        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

Remember to put a default route aswell but under your custom route...

And remember to change your controller aswell

Upvotes: 1

Related Questions