简体   繁体   English

如何通过url参数路由?

[英]How pass url params to route?

I add route to project.All work correct, but if i send normal url as first param its not correctly work. 我将路由添加到project.All工作正常,但是如果我将正常的url作为第一个参数发送,则无法正常工作。

Get["/{url}/{digit}"

If i send this params to server-all work correctly. 如果我将此参数发送到服务器-所有工作正常。

localhost:8888/google.com/2

But if i send param with http://www its not work. 但是,如果我使用http://www发送param,则无法正常工作。

localhost:8888/https://www.google.com/2

How correct pass url param to route? 如何正确传递url参数以进行路由? I think it because Nancy think that i send 3 input param. 我认为是因为Nancy认为我发送了3输入参数。

如果您确实需要使用GET而不是POST,请尝试HttpUtility.UrlEncode("https://google.com/2")对您的网址进行网址编码。

You have to encode your url which is send as a paramater: 您必须对作为参数发送的网址进行编码:
Use: 采用:

var encodedString = Uri.EscapeDataString("https://www.google.com/2");

then your url will look like this and it shouldn't get any errors: 那么您的网址将如下所示,并且不会出现任何错误:

https%3A%2F%2Fwww.google.com%2F2 HTTPS%3A%2F%2Fwww.google.com%2F2

Sending the request: 发送请求:

localhost:8888/https%3A%2F%2Fwww.google.com%2F2 本地主机:8888 / HTTPS%3A%2F%2Fwww.google.com%2F2

Or ou can use the 或者您可以使用

HttpUtility.UrlEncode();

method. 方法。 For further information have a look at this . 有关更多信息,请查看

Since you insist on changing the backend only, you could try using a regex to capture your route 由于您坚持只更改后端,因此可以尝试使用正则表达式来捕获您的路由

Get["^(?<url>.*<digit>[0-9]+)$"]

This should match any url ending with atleast one number, and put everything before it in url like so: 这应该匹配以至少一个数字结尾的任何url,并将所有内容放在url中,如下所示:

Get["^(?<url>.*<digit>[0-9]+)$"] = parameters =>
    {
        var url = parameters.url;
        var digit = parameters.digit;
    };

I am currently unable to verify if this works as you want it to though, and to make sure you can adjust this yourself make sure to look into how to write regex 我目前无法验证它是否可以按您的意愿运行,并且要确保您可以自行调整,请确保研究如何编写正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM