ASP.NET MVC 路由 - 重定向到 aspx?
这看起来应该很容易,但由于某种原因我没有运气。我正在将现有的 WebForms 应用程序迁移到 MVC,因此我需要暂时保留指向现有 aspx 页面的站点根目录,并且仅将路由应用于命名路由。这就是我所拥有的:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
RouteTable.Routes.Add(
"Root",
new Route("", new DefaultRouteHandler())
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Calendar2", action = "Index", id = "" } // Parameter defaults
);
}
所以 aspx 页面应该被忽略,默认的根 url 应该由这个处理程序处理:
public class DefaultRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(
"~/Dashboard/default.aspx", typeof(Page)) as IHttpHandler;
}
}
这似乎工作正常,但生成的 YPOD 给了我这个:
多个控件具有相同的ID 找到“__Page”。跟踪需要 该控件具有唯一的 ID。
这似乎意味着该页面以某种方式被渲染了两次。如果我直接输入仪表板页面的 url,它就可以正常工作(没有路由,没有错误)。我不知道为什么处理程序代码会做不同的事情。
底线——我想简单地将根 url 路径重定向到我选择的 aspx——任何人都可以透露一些信息吗?
This seems like it should be easy, but for some reason I'm having no luck. I'm migrating an existing WebForms app to MVC, so I need to keep the root of the site pointing to my existing aspx pages for now and only apply routing to named routes. Here's what I have:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
RouteTable.Routes.Add(
"Root",
new Route("", new DefaultRouteHandler())
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Calendar2", action = "Index", id = "" } // Parameter defaults
);
}
So aspx pages should be ignored, and the default root url should be handled by this handler:
public class DefaultRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(
"~/Dashboard/default.aspx", typeof(Page)) as IHttpHandler;
}
}
This seems to work OK, but the resulting YPOD gives me this:
Multiple controls with the same ID
'__Page' were found. Trace requires
that controls have unique IDs.
which seems to imply that the page is somehow getting rendered twice. If I simply type in the url to my dashboard page directly it works fine (no routing, no error). I have no idea why the handler code would be doing anything differently.
Bottom line -- I'd like to simply redirect the root url path to an aspx of my choosing -- can anyone shed some light?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太明白你的问题,可能是我技术不够。无论如何,我会尝试...;)
如果您想从根路径重定向,为什么不使用IIS的默认文档,例如将其放入index.aspx,然后在此页面中添加response.redirect到您要访问的页面想要重定向到?
I do not really understand your problem , may be I am not skilled enough. Anyway I will try ... ;)
If you want to redirect from the root path, why not using the default document from IIS, put it to index.aspx for example and then add in this page a response.redirect to the page you want to redirect to?
嗯嗯。所以我的浏览器由于一些不相关的原因崩溃了,现在我已经重新启动它,上面的代码似乎按预期完美工作。我完全困惑于服务器端代码现在会因为浏览器重新启动而表现出不同的行为(显然我猜有些东西被错误地缓存了),但看来这现在不是问题了。
编辑:嗯,这实际上仍然是一个问题 - 不确定为什么它以前有效,但它有点随机。最重要的是,在某些情况下,MVC 似乎无法与 Trace.axd 解析器很好地配合,否则会产生完全有效的标记。我确实没有很好的解释,但在 web.config 中禁用跟踪可以绕过该错误。就我目前的目的而言,这已经足够好了,但我很想听到其他人更好的解释......
Hmmmm. So my browser crashed for some unrelated reason, and now that I have restarted it, the above code seems to be working perfectly as expected. I'm completely confused about how server-side code would now be acting differently because of a browser restart (apparently somehow something was cached incorrectly I guess), but it appears that this is now a non-issue.
EDIT: Well, this is actually still an issue -- not sure why it worked before, but it's a bit random. The bottom line seems to be that MVC does not appear to play nicely with the Trace.axd parser in some circumstances that otherwise produce perfectly valid markup. I really don't have a good explanation, but disabling tracing in web.config bypasses the error. For my purposes for now that's good enough, but I'd love to hear a better explanation from someone else...