这一定是以前被问过的,但是在阅读了这里、这里、这里和这里之后,我无法推断相关部分以使其工作。我正在将一个旧的 Web 表单网站改造成 MVC,并希望捕获特定的传入 HTTP 请求,以便我可以发出RedirectPermanent(以保护我们的 Google 排名并避免用户因 404 问题而离开)。
我不需要拦截所有传入请求或解析某些id值,而是需要拦截以.aspx文件扩展名结尾(或包含)的所有请求,例如
www.sample.com/default.aspx
www.sample.com/somedir/file.aspx
www.sample.com/somedir/file.aspx?foo=bar
Run Code Online (Sandbox Code Playgroud)
对 MVC 路由的请求应被忽略(只是正常处理)。
这是我到目前为止所拥有的,除了ASPXFiles路线从未被击中。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// never generates a match
routes.MapRoute(
name: "ASPXFiles",
url: "*.aspx",
defaults: new { controller = "ASPXFiles", action = "Index" }
);
// Used to process all other requests (works fine)
routes.MapRoute( …Run Code Online (Sandbox Code Playgroud) 知道了RedirectToAction,我正在寻找类似的东西,以保持用户的URL稳定,并仍然将责任从一个动作传递到另一个动作.
由于我在这个主题上找到了零搜索结果,我不妨完全尝试解决XY问题.
不过,我会试着解释为什么我认为可能需要这个.
场景:
public ActionResult A(int id)
{
var uniqueID = CalculateUniqueFromId(id);
// This compiles.
return RedirectToAction("B", new { uniqueID });
// This does not compile.
return RewriteToAction("B", new { uniqueID });
}
public ActionResult B(Guid uniqueID)
{
var model = DoCreateModelForUnique(uniqueID);
return View("B", model);
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,动作A从整数计算一个guid并将其传递给另一个动作.
解决方法:
我可以将上面的代码更改为:
public ActionResult A(int id)
{
var uniqueID = CalculateUniqueFromId(id);
var model = DoCreateModelForUnique(uniqueID);
return View("B", model);
}
public ActionResult B(Guid uniqueID)
{
var model …Run Code Online (Sandbox Code Playgroud) 我正在构建的ASP.NET应用程序中实现企业库异常处理应用程序块.我打算通过在Global.asax.cs中放置以下代码来处理未捕获的应用程序异常:
protected void Application_Error()
{
Exception error = Server.GetLastError();
Exception errorToThrow;
if (ExceptionPolicy.HandleException(error, "Application Error", out errorToThrow))
{
if (errorToThrow != null)
throw errorToThrow;
}
else
Server.ClearError();
}
Run Code Online (Sandbox Code Playgroud)
我相信这将有助于处理策略的各种后处理操作(None,NotifyRethrow,ThrowNewException),但我想知道是否有人发现此实现存在重大问题.
我有以下内容IHttpModule,我正在试图弄清楚如何从控制器为给定的绝对或相对URL执行操作.
public class CustomErrorHandlingModule : IHttpModule
{
#region Implementation of IHttpModule
public void Init(HttpApplication context)
{
context.Error += (sender, e) =>
OnError(new HttpContextWrapper(((HttpApplication)sender).Context));
}
public void Dispose()
{}
public void OnError(HttpContextBase context)
{
// Determine error resource to display, based on HttpStatus code, etc.
// For brevity, i'll hardcode it for this SO question.
const string errorPage = @"/Error/NotFound";
// Now somehow execute the correct controller for that route.
// Return the html response.
}
}
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?