The route is mapped to different route parameters

I am trying to configure some routes for my ASP.NET MVC 5 project.

Now I have strange behavior:

  • /Home/About correctly routed
  • /Home/Index redirected to /XmlRpc?action=Index&controller=Blog
  • /Home/Index works (yes, I found that because of a typo) - did I always think that routes are case insensitive?
  • Usage Url.Action("Foo","Bar")also creates/XmlRpc?action=Foo&controller=Bar

This is my file RouteConfig:

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

    routes.Add("XmlRpc", new Route("XmlRpc", new MetaWeblogRouteHandler()));

    routes.MapRoute("Post", "Post/{year}/{month}/{day}/{id}", new {controller = "Blog", action = "Post"}, new {year = @"\d{4,4}", month = @"\d{1,2}", day = @"\d{1,2}", id = @"(\w+-?)*"});
    routes.MapRoute("Posts on Day", "Post/{year}/{month}/{day}", new {controller = "Blog", action = "PostsOnDay"}, new {year = @"\d{4,4}", month = @"\d{1,2}", day = @"\d{1,2}"});
    routes.MapRoute("Posts in Month", "Post/{year}/{month}", new {controller = "Blog", action = "PostsInMonth"}, new {year = @"\d{4,4}", month = @"\d{1,2"});
    routes.MapRoute("Posts in Year", "Post/{year}", new {controller = "Blog", action = "PostsInYear"}, new {year = @"\d{4,4}"});
    routes.MapRoute("Post List Pages", "Page/{page}", new {controller = "Blog", action = "Index"}, new {page = @"\d{1,6}"});
    routes.MapRoute("Posts by Tag", "Tag/{tag}", new {controller = "Blog", action = "PostsByTag"}, new {id = @"(\w+-?)*"});
    routes.MapRoute("Posts by Category", "Category/{category}", new {controller = "Blog", action = "PostsByCategory"}, new {id = @"(\w+-?)*"});

    routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Blog", action = "Index", id = UrlParameter.Optional});            
}

And this definition MetaWeblogRouteHandler:

public class MetaWeblogRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MetaWeblog();
    }
}

Id ASP.NET MVC (/controller/action) + + XML-RPC XmlRpc /XmlRpc.

, Default, , .
?

Update:
/Home/Index AppRelativeCurrentExecutionFilePath "~/XmlRpc", XmlRpc . , - ?

Update2: ​​ , : IE Visual Studio . (, , , IE VS = , ). , , ;)

+4
1

Url.Action("Foo","Bar"), MVC ( action = Foo, controller = Bar), , , .

XmlRpc . , URL- @Url.Action, @Html.ActionLink ..

URL- ( , , ). :

routes.Add("XmlRpc", new Route("XmlRpc", new RouteValueDictionary() { { "controller", "XmlRpc" } }, new MetaWeblogRouteHandler())); 

, Url.Action("Foo","Bar"), /Bar/Foo url, "Bar" "XmlRpc".

.

RouteBase. url /XmlRpc, MetaWeblogRouteHandler - URL-:

public class XmlRpcRoute : RouteBase
{
    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        //The route will only be a match when requesting the url ~/XmlRpc, and in that case the MetaWeblogRouteHandler will handle the request
        if (httpContext.Request.AppRelativeCurrentExecutionFilePath.Equals("~/XmlRpc", StringComparison.CurrentCultureIgnoreCase))
            return new RouteData(this, new MetaWeblogRouteHandler());

        //If url is other than /XmlRpc, return null so MVC keeps looking at the other routes
        return null;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {            
        //return null, so this route is skipped by MVC when generating outgoing Urls (as in @Url.Action and @Html.ActionLink)
        return null;
    }
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    //Add the route using our custom XmlRpcRoute class
    routes.Add("XmlRpc", new XmlRpcRoute());

    ... your other routes ...
}

IHttpHandler MVC URL-. , MVC, , URL- .

web.config, /XmlRpc MVC:

<configuration>
  ...
  <system.webServer>
    <handlers>
      <!-- Make sure to update the namespace "WebApplication1.Blog" to whatever your namespace is-->
      <add name="MetaWebLogHandler" verb="POST,GET" type="WebApplication1.Blog.MetaWeblogHandler" path="/XmlRpc" />
    </handlers>
  </system.webServer>
</configuration>

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    //Make sure MVC ignores /XmlRpc, which will be directly handled by MetaWeblogHandler
    routes.IgnoreRoute("XmlRpc");

    ... your other routes ...         
}

, :

  • /Home/Index HomeController

  • / BlogController

  • @Url.Action("Foo","Bar") URL /Bar/Foo

  • @Html.ActionLink("MyLink","Foo","Bar") html: <a href="/Bar/Foo">MyLink</a>

  • /XmlRcp , MetaWeblogHandler , (blog.index, )


, MVC 5, NuGet xmlrpcnet-server.

HomeController BlogController, , MetaWeblog:

public interface IMetaWeblog
{
    [XmlRpcMethod("blog.index")]
    string Index();        
}

public class MetaWeblogHandler : XmlRpcService, IMetaWeblog
{
    string IMetaWeblog.Index()
    {
        return "Hello World";
    }        
}

public class MetaWeblogRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MetaWeblogHandler();
    }
}
+5

Source: https://habr.com/ru/post/1535818/


All Articles