0

I'm trying to understand the behavior within my RouteConfig setup. Here is what I have:

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

        routes.MapRoute(
            name: "2KeywordController",
            url: "{keyword1}-{keyword2}-{controller}/{action}",
            defaults: new { action = "Index" }
        );

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

I have a controller called ContactController and a View under /Views/Contact/Index.cshtml that has the following to create the form:

@using (Html.BeginForm("Index", "contact", FormMethod.Post, new { id = "contactform" }))

When I navigate to example.com/kw1-kw2-contact the ContactController is correctly called and the default contact view is displayed. When I looked at the source I was surprised to find that the action for the form was set to "/kw1-kw2-contact" instead of just "/contact". Is there a way to use Html.Begin() but have just the controller name appear in action without the two keywords? e.g. /contact

4

1 回答 1

3

有没有办法使用 Html.Begin() 但只有控制器名称出现在没有两个关键字的情况下?

当然,您可以使用 aBeginRouteForm而不是 aBeginForm并指定要使用的路由名称:

@using (Html.BeginRouteForm(
    routeName: "Default", 
    routeValues: new { action = "index", controller = "contact" }, 
    method: FormMethod.Post, 
    htmlAttributes: new { id = "contactform" })
)
{
    ...
}

将发出:

<form action="/contact" id="contactform" method="post">
    ...
</form>
于 2013-02-04T21:25:59.623 回答