URL 路由 C# mvc 和 Web 窗体

URL Routing C# mvc and Web Forms(URL 路由 C# mvc 和 Web 窗体)
本文介绍了URL 路由 C# mvc 和 Web 窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 webforms 和一个 mvc 应用程序,我正在尝试正确路由.我的默认路由按预期工作,但是当我单击其中一个视图中的操作链接时,它没有路由到正确的页面.

So I have a webforms and an mvc application combined and am trying to get things routed correctly. I have the default routing working as expected, but when I click on an actionlink in one of my views, it is not routing to the correct page.

这是我的路由代码.

void RegisterRoutes(RouteCollection routes) {
           routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

           routes.MapPageRoute("",
               "", "~/Default.aspx", true);

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Chips", action = "Index", id = UrlParameter.Optional }
            );



        }


        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup

           AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

        }

这是我会点击的操作链接:@Html.ActionLink("Properties Editor", "Index", "Property")

Here's an Action Link that I would click on:@Html.ActionLink("Properties Editor", "Index", "Property")

这是我的预期结果:urlgoeshere.com/Property/Index

这是我的实际结果:urlgoeshere.com/?action=Index&controller=Property

我不知道要改变什么来补救这种情况?有什么想法吗?

I'm not sure what to change to remedy this situation? Any ideas?

推荐答案

我最终不得不添加路由约束.这就是我最终做的事情.

I ended up having to add a routing constraint. Here's what I ended up doing.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

          routes.MapPageRoute("",
               "", "~/Default.aspx", true, null, new RouteValueDictionary { { "outgoing", new PageConstraint() } });


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Chips", action = "Index", id = UrlParameter.Optional }
            );

还有页面约束.

public class PageConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (routeDirection == RouteDirection.IncomingRequest)
                return true;
            return false;
        }
    }

这篇关于URL 路由 C# mvc 和 Web 窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)