我们有一个现有的ASP.NET Web窗体应用程序,我们正在尝试确保将使用ASP.NET MVC技术创建任何新的开发(新页面)。
对于那些混合使用ASP.NET Web表单和ASP.NET MVC的人来说,以下编程代码将为您所熟悉。 我已经修改了global.asax.cs C#文件,以便调用RouteConfig的RegisterRoutes
protected void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
在RouteConfig.cs中,我具有以下内容:
public static void RegisterRoutes(RouteCollection routes)
{
// Reference: https://stackoverflow.com/questions/2203411/combine-asp-net-mvc-with-webforms
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// This informs MVC Routing Engine to send any requests for .aspx page to the WebForms engine
routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}");
routes.IgnoreRoute("{myWebForms}.aspx");
routes.IgnoreRoute("{myWebServices}.asmx/{*pathInfo}");
routes.IgnoreRoute("myCustomHttpHandler.foo/{*pathInfo}");
routes.IgnoreRoute("Contents/{*pathInfo}");
// Important change here: removed 'controller = "Home"' from defaults. This is the key for loading
// root default page. With default setting removed '/' is NOT matched, so default.aspx will load instead
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
}
好消息是,我可以轻松地从ASP.NET Web窗体页面导航到ASP.NET MVC视图。
不幸的是,从ASP.NET MVC视图转到ASP.NET Web窗体页面将不起作用,因为我们具有以下指定无cookie的ASP.NET窗体身份验证的配置
<authentication mode="Forms">
<forms name=".ClarityCore" loginUrl="Login.aspx" defaultUrl="SelectCompany.aspx" cookieless="UseUri" />
</authentication>
因此,我们的登录页面(这是一个ASP.NET Web表单页面)具有如下所示的URL:
在目标网页的Dashboard.aspx中,以下链接将使我们可以轻松导航至ASP.NET MVC视图:
<asp:HyperLink ID="test" runat="server" Text="Go to ASP.NET MVC world" NavigateUrl="Home/Index" />
但是,在ASP.NET MVC视图中,我放置了以下代码:
<a href="@Url.Content("~/Dashboard.aspx")">Back to ASP.NET Web
Forms</a>
,浏览器中的网址变为:
http://localhost:50000/Login.aspx?ReturnUrl=%2fDashboard.aspx
我假设这是因为ASP.NET MVC未配置为使用无cookie表单身份验证。 另外,重要说明:我们的ASP.NET Web窗体和ASP.NET MVC位于单个ASP.NET Web应用程序项目中
我必须采取什么步骤来确保从ASP.NET MVC视图导航到ASP.NET Web窗体页面将在使用无cookie的ASP.NET窗体身份验证的Web应用程序中正常工作?
谢谢。