How can I redirect to "page not found" with MVC3?
I have some links in Google that I now no longer expect to work. All of the links look like this:
www.abc.com/xx/que=xxxxxxxxxxx
Where x can be anything.
开发者_如何学GoCan someone tell me how I can set up a route and controller action that will return a 404 to google? I guess I need to set it up with a mask that includes "que" but I am not so sure how to do this.
Add a new route to the top of your global.asax. This will catch requests of the form xx/que={anything}
using a regular expression to define the "que" argument.
routes.MapRoute(
"PageNotFound",
"xx/{que}",
new { controller = "Error", action = "NotFound" },
new { que = "que=.*" });
This would also assume you have an ErrorController
with action NotFound
and corresponding view named NotFound.aspx
in your /Views/Error/ directory.
public class ErrorController : Controller
{
public ActionResult NotFound()
{
Response.StatusCode = 404;
return View();
}
}
精彩评论