Вопрос

I have the following map route in my route config. This is the first route before anything else.

routes.MapRoute(
                "HomePage",
                "",
                new { area = "Accessibility", controller = "Cardholders", action = "Index" }
                );

However, when I view my website in browser, I get

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Cardholders/Index.aspx
~/Views/Cardholders/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Cardholders/Index.cshtml
~/Views/Cardholders/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

I have no issues when I browse my action directly http://localhost:54358/accessibility/cardholders/index

What I want to achieve, is to type http://localhost:54358 and it redirects to http://localhost:54358/accessibility/cardholders/index

Based on the answers below, I have tried

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Cardholders", action = "Index", id = UrlParameter.Optional},
                // Parameter defaults
                new[] { "Plan.Web.Mvc.Areas.Accessibility.Controllers" }
                );

and

routes.MapRoute(
                "HomePage",
                "Accessibility_Default",
               "Accessibility/{controller}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
                );

and

routes.MapRoute(
                "HomePage",
                "Accessibility_Default",
               "Accessibility/{controller}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
            );

All don't seem to work.

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

Решение

I'll try answering my own question.

It's sort of a workaround, and I hope this helps.

I created a new Area called "Home", and within this area, a controller called "HomeController".

In the area registration of "Home", I have this

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute("", "", defaults: new { controller = "Home", action = "Index", area = "Home" });

    context.MapRoute(
        "Home_default",
        "Home/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

And in my HomeController, I have this

    public ActionResult Index()
    {
        return RedirectToAction("Index", "Cardholders", new { area = "Accessibility" });
    }

Другие советы

Try Something like this

routes.MapRoute(
                "HomePage",
                "Accessibility_Default",
               "Accessibility/{controller}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
                );

Hope this will help you.

For more info please follow here and here

Try this

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "controller Name", action = "action name", id = UrlParameter.Optional }, // Parameter defaults
            new[] { "Your Area Controller Namespace" }
        );

Give this a go, you will just need to replace MyApp with the name of your project.

Example

routes.MapRoute(
    "HomePage",
    "",
    new { action = "Index" },
    new { controller = "Cardholders" },
    new[] { "MyApp.Areas.Accessibility.Controllers" }
);

When you are testing it put the route at the top so that you know no other routes are interfering with the result.

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