我有一个简单的表单,它以
看起来我在使用
表格:
HttpPost
操作方法作为目标,提交后返回其对应的视图。我的问题是,我得到了404 Not Found
错误。奇怪的是,如果我将表单方法和操作方法上的属性更改为GET
,那么它就可以正常工作并显示TestMethod
视图。看起来我在使用
POST
时漏掉了什么,但我的其他控制器中的帖子都很正常(例如账户登录和注册)。请注意,AllowAnonymous
属性是一个自定义属性,用于指定允许匿名访问的控制器或操作,而不是通过Authorize
属性指定需要授权的控制器或操作。我猜没有什么是不可能的,但我认为这与我的问题无关。你认为出了什么问题吗?表格:
@using (Html.BeginForm("TestMethod", "Test", FormMethod.Post, new { @id = "testForm" })) {
<fieldset>
<legend>Test Form</legend>
<input type="submit" value="Submit" />
</fieldset>
}
控制器操作:
[AllowAnonymous]
[HttpPost]
public ActionResult TestMethod() {
return View();
}
查看:
<h2>TestMethod</h2>
<p>HttpPost method was successful.</p>
从 Global.asax.cs 注册路由方法:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// About
routes.MapRoute(
"About", // Route name
"about", // URL with parameters
new { controller = "Home", action = "About" } // Parameter defaults
);
// Faq
routes.MapRoute(
"Faq", // Route name
"faq", // URL with parameters
new { controller = "Home", action = "Faq" } // Parameter defaults
);
// Glossary
routes.MapRoute(
"Glossary", // Route name
"glossary", // URL with parameters
new { controller = "Home", action = "Glossary" } // Parameter defaults
);
// Register
routes.MapRoute(
"Register", // Route name
"register", // URL with parameters
new { controller = "Account", action = "Register" } // Parameter defaults
);
// LogIn
routes.MapRoute(
"LogIn", // Route name
"login/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"LogOn", // Route name
"logon/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);
// Default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
授权属性代码:
// AllowAnonymousAttribute class
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AllowAnonymousAttribute : Attribute { }
// GlobalAuthorize class
public sealed class GlobalAuthorize : AuthorizeAttribute {
public override void OnAuthorization(AuthorizationContext filterContext) {
bool skipAuthorization =
filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
if (!skipAuthorization) base.OnAuthorization(filterContext);
}
}
// RedirectAuthorizeAttribute class
public class RedirectAuthorizeAttribute : AuthorizeAttribute {
public string RedirectUrl { get; set; }
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) {
filterContext.Result = new RedirectResult(RedirectUrl);
}
}
全局过滤器:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new RequireHttpsAttribute());
filters.Add(new GlobalAuthorize());
filters.Add(new HandleErrorAttribute());
}
路由重写规则:
<rewrite>
<rules>
<!-- Block all requests made to a website that do not have the host header set. -->
<rule name="Fail bad requests" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
</conditions>
<action type="AbortRequest" />
</rule>
<!-- Remove trailing slash from all incoming requests. -->
<rule name="Remove trailing slash" stopProcessing="false">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<!-- Convert all requests to all lowercase. -->
<rule name="Convert to lowercase" stopProcessing="false">
<match url=".*[A-Z].*" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
<!-- Any URL with (HTTPS == OFF) and (HTTP_HOST with colon) -> use for development testing. -->
<rule name="Development redirect to HTTPS" enabled="true" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
</conditions>
<action type="Redirect" url="https://{C:1}:44300{URL}" />
</rule>
<!-- Redirect any HTTP request to HTTPS. -->
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
AllowAnonymous
属性吗? - twoflowerAllowAnonymous
属性,我将被重定向到登录页面(这是期望的行为)。 - neizanAllowAnonymous
属性的代码,以及全局过滤器。 - neizanRegisterRoutes
方法还是路由重写规则?如果有帮助的话,我已经在Web.config
中发布了路由重写规则。如果我误解了,请澄清一下,我会再发更多信息。谢谢。 - neizanPOST
请求在某个地方被更改为GET
请求。有人遇到过类似的问题吗?有人知道如何解决吗?如果我保留表单上的FormMethod.Post
并只更新控制器操作为HttpGet
,它可以工作,我认为这是由于POST
被更改为GET
。救命啊!谢谢! - neizan