NET Core application WebMethod needs to pass in URL as parameter - webmethod

My NET Core application exposes a Webmethod.
This method needs to accept a URL as parameter for further processing.
e.g.
mysite.com/webmethod/url=https://www.google.com
It would never go through.
Instead, it shows Page Not Found.
It seems this webmethod will never be routed to my controller if there is a forward slash in the URL.
However, after getting rid of forward slash, it works very well.
mysite.com/webmethod/url=https:www.google.com
Could someone tell me how to deal with forward slash?
Thank you.

You can use UrlEncode and UrlDecode.
var queryString = UrlEncode("https://google.com"); // https%3A%2F%2Fgoogle.com
var decodedString = UrlDecoce(queryString); // https://google.com

Related

how to get full url with # in laravel

I am using laravel 5 and I am trying to get my full url which is
/faq#manual
However I can only get /faq
when I call the function
Request::url() or Request::fullUrl()
is there any way I can get the full url
with #manual?
It is impossible to get the fragment from a URL with Standard HTTP. So using variables like $_SERVER or $_GET can't help you. Actually, the server never knows about this value because the browser won't even send a request with a fragment part.
If you want to have the fragment value in the server side, you should get the value by javascript first then pass it as a URL parameter and get it by $_GET in your server side.
That's not possible in http or php however you can use javascript to get the full url including the hash.
var url = document.URL;
or
var url = window.location.href;
Hope this helps.

How to hide function and parameter from url in codeigniter?

this is my URL.
http://localhost/savyy/home/home_page/Islamabad
i want to show it like
http://localhost/savyy/home
i m using uri routing code
$route['home/(:any)'] = 'home/home_page/$1;
but nothings happen...it shows same URL. can anyone please help me ? please tell me how to hide function name and parameter.
I dont think it will work that way, the request is the route to controller. the request uri will be the same. you will still have the "home/home_page/islamabad" in your url. unless you redirect with the home_page() to home() and save the islamabad in a session.

Pass special character through query string in ASP.NET MVC 4

When I try to pass some special characters, though query string in the URL property of the Ajax post method, it seems to be some error happened. That means I won't get correct special characters when hitting controller action in ASP.NET MVC 4.
My code is
var temp=temp##$#%#%#979
url=(Controller/Action/id/name?departname=+temp);
request from a script function from a cshtml formajax
If you are making your ajax request from javascript, then use encodeURIComponent:-
var temp = encodeURIComponent('temp##$#%#%#979'); //temp%40%23%24%23%25%23%25%23979
use HttpUtility.UrlEncode for encoding the parameter
var temp = temp##$#%#%#979
HttpUtility.UrlEncode(temp)
url = (Controller/Action/id/name?departname=+temp);
and use Server.UrlDecode for decoding the parameter in controller
var Depart = Server.UrlDecode(Request.QueryString["DepartmentName"]);
I'm not familiar with ASP.net, however most languages require you to URL encode strings that are meant to be used as part of a URL.
For example:
https://msdn.microsoft.com/en-us/library/4fkewx0t%28v=vs.110%29.aspx
I hope this helps get you moving in the right direction.

Doing a URL rewrite in a ServletFilter using Jetty

I need to do a URL rewrite in a ServletFilter so that "foo.domain.com" gets rewritten to "foo.domain.com/foo". I'm using Jetty, which has a handy way of modifying requests: just cast the request to a Jetty Request object and you get a bunch of setters which allow you to modify it. Here's my code (which doesn't work):
String subdom = Util.getSubDomain(req);
org.eclipse.jetty.server.Request jettyReq = (Request) req;
String oldUri = jettyReq.getRequestURI();
String newUri = "/" + subdom + oldUri;
jettyReq.setRequestURI(newUri);
My purpose is to serve files out of the /foo directory, which lives at /webapps/root/foo.
I'm guessing that I also need to call things like setContextPath(), setPathInfo(), setURI(), setServletPath(), and who knows what else.
What's the magic combination that will make it look like the original request was for /foo?
Edit: to clarify, the reason I say the code doesn't work is that files are still being served out of /webapps/root, not /webapps/root/foo.
Just use the rewrite handler, we have support for what you're trying to do:
http://wiki.eclipse.org/Jetty/Feature/Rewrite_Handler
Answering my own question: I was missing
jettyReq.setServletPath(newUri);
Add that and everything works.

CodeIgniter routing problem. (appends ajax route to existing url)

I'm trying to perform an AJAX-request in a view, the user gives some input which is sent to the server with AJAX and the function it's supposed to go to is routed with CodeIgniters routes.
This is the view I'm currently standing in while making the request.
http://localhost:8888/companies/list
In my route config I've set this route below to handle the AJAX-request, which should be able to come from any view and still be able to go to the route I've specified.
$route['test_ajax'] = "ajax/test_ajax";
So the request should go to the "ajax"-controller and use the function "test_ajax", which should make the POST-url look like this.
POST http://localhost:8888/test_ajax
But instead what I get is the current URL I'm standing at, and the route I've specified appended to the URL crashing my response from the AJAX-request completely since it didn't even go close to the function it's supposed to. The POST-url I get looks like this.
POST http://localhost:8888/companies/test_ajax
Notice how the parameter of /companies was removed. The argument /list was lost somewhere, al though if I add a trailing slash after the list I get the list argument in the URL as well.
So what just happened is that the POST tries to go to the companies-controller and look for the function test_ajax which is defined in the ajax-controller and not in the companies-controller. This error keeps occuring no matter what URL I'm at, and it always follows the same pattern. It keeps appending my route-URL to the existing URL instead of routing correctly.
So what could be causing the routing to behave this way, is there any setting that's accidently enabled or anything? Because I know I've got this to work hundreds of times in previous projects.
Thanks in advance.
It is because your Javascript is using the current directory as the base, and appending the AJAX URL to it. Because you are (to the client-side at least) in the companies directory, it appends your URL onto this.
The solution, if your Javascript is inline, is to just use the base_url() PHP function wihtin the code ...
var url = '<?= base_url(); ?>test_ajax/'
If your Javascript is not inline, you can declare a global variable at the top of your HTML document using the PHP function...
var BASE_URL = '<?= base_url(); ?>'
And use it everywhere else in your Javascript ...
var url = BASE_URL + 'test_ajax/'
Alternatively, you could just hardcode your base URL, but that could get real messy real quick.
Turns out, CodeIgniter interpreted this as a relative link due to the fact that there was no heading slash. CodeIgniter User-Guide states that no heading or trailing slashes should be written in the routes config.
What solved this though was adding a heading slash in the java-URL.
$.ajax({
url: "/test_ajax",
type: "POST",
data: data,
success: function(data){
console.log(data);
}
});
This forces CI to interpret this as a absolute URL and gives me the URL I was looking for.
POST http://localhost:8888/test_ajax

Resources