Вопрос

Keep in mind I am new to asp.net mvc, so I am aware that maybe my problem is really easy to solve. :)

I started a new ASP.NET project based on the MVC pattern si O got the whole HomeController and working website. So I added a new Controller (CoursesController) and a new route. But it's not working.

To show some of the work.

CoursesController.cs - Part of it

public class CoursesController : Controller
{
    private readonly CourseStore _store = new CourseStore();

    //
    // GET: /Course/
    public ActionResult Index()
    {
        return View(_store.GetCourses());
    }

RouteConfig.cs

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Courses",
            url: "Courses/{action}/{courseid}",
            defaults: new { controller = "Courses", action = "Index", courseid = UrlParameter.Optional }
        );

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

_Layout.cshtml - Relevant link section

<ul class="nav navbar-nav">
    <li>@Html.ActionLink("Home", "Index", "Home")</li>
    <li>@Html.ActionLink("About", "About", "Home")</li>
    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
    <li>@Html.ActionLink("Courses", "Index", "Courses")</li>
</ul>

Result If I click the link in _layout I am sent to the url http://localhost/Courses/ and I get a reply You do not have permission to view this directory or page.. But if I change the url manually to http://localhost/Courses/Index everything works fine.

Keep in mind the ActionLink only generates http://localhost/Courses/

Any help is greatly appreciated.

Это было полезно?

Решение

The following is pasted from user WeTTTT as was the correct answer.

I believe you have a folder "courses" under your site root directory, which will cause the url doesn't hit mvc routing at all. An quick way to solve this is to add routes.RouteExistingFiles = true;, but that's not suggested and may cause other issues. Do you want/allowed to change the "Courses" folder name to something else that get around the routing? – WeTTTT Mar 21 at 12:11

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top