A thirdparty is calling the following URI but I'm unable to retreive the parameters :
http://host:port/#property1=abc&property2=123
I created an AspNetCore WebApi controller with an empty controller name and an empty route name.
So far, I can receive the call but I'm unable to read the property1 and property2.
Those are not parameters from body, nor from Route/Query.
Last resort would be to retreive the full received URI and parse it with a regex but I'm also not able to (I tried to setup an ActionFilterAttribute but I'm stuck getting the called URI).
So far this is exceeding my webapi knowledge.
Thank you
No... hash parameters are not sent so server and is only used by the browser.
You can use ? Instead of # to get it on the server.
Related
For some days now i have been struggling on a project where i need to provide a URL to enable me to receive a JSON response(precisely this my first time of calling a web API)
This is the scenario:
- First the company has a web service that i need to consume and send a bill prompt to our client and so far it has been successful
- Second , they ask me to implement a callback and give them the URL where they will call me to send the status of the bill whether our client has confirm the bill prompt or not(this is where i am stacked)
When i get the JSON data i can easily use it to do what i want but my problem is how to implement the web page that the company will call to send the status.
Please a sample code will help me a lot.Thanks
What you are trying to achieve is called WebAPI. You expose HTTP endpoints using Controllers to the Internet and consumers of your API may utilize them to POST the status back.
You are describing creating an API. Basically, you create an endpoint url using a controller in C#. It's within this controller that you receive data from "the company" and process it, and do whatever you need to do after that. If you use the standard MVC framework build into C#, it's fairly straight-forward. Here is an example of a callback url we are using in a three-legged OAuth procedure. The last step involves a third party sending a request to our callback url and posting some data. We are using a model as a parameter. But you can use simple structures (int, string, etc) as well. As log as the names of your params match the names that the third party sends in their query string or POST, the MVC framework will handle the variable assignment automatically. The serialization/deserialization is built in to the framework.
Here is a small example of how we have implemented a callback url in one of our apps:
public class MyAuthenticatedController : Controller
{
public ActionResult Index([FromUri]MyAuthenticatedModel model)
{
logTheResponse(model);
if (model == null)
{
throw new HttpException(401, "Auth Failed");
}
}
}
The third party would hit the url:
http://app.mydomain.com/myauthenticated
Using the GET method.
If you don't want to build an ASP.NET Web API Project, you can do this easily with an Azure Function.
See: https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-generic-webhook-triggered-function
I have implemented Web API Controller with PUT method (I marked method with HttpPut attribute from System.Web.Http).
And when I try to make put request, i have message
The requested resource does not support http method 'PUT'.
I remove webdav in Web.Config, added put etc.
But it is still not work.
How I can resolve this problem?
Make sure that the param names on your URL match with some param names in your controller method declaration. This simple mistake can cause 405.
In c# MVC4 how to I get the server path. for example: http://192.169.1.120:60632
Is there a helper function that I can convert something like ~/aFolder/file.htm into an absolute path? Ideally I would like a way of taking any given url and convert into a full absolute url. E.g. can cope with..
/aFolder/file.html -> http://192.169.1.120:60632/aFolder/file.html
http://website.com/file.html -> http://website.com/file.html
And will work anywhere within the c# code - i.e. in a action controller, signalR hub, model etc.
And it will still work when I deploy to a remote server.
Use the Request.Url property available in Controller. This returns a Uri object containing info on the request. From there, you can access the AbsoluteUri and Port properties to get the info that you need.
If you are interested in getting the url info from SignalR, try looking at this question and answer.
Try this one,
System.Web.HttpContext.Current.Server.MapPath(#"~/FolderName")
You can try these as well
string path = AppDomain.CurrentDomain.GetData("FolderName").ToString();
HostingEnvironment.MapPath(#"~/FoldeName");
In MVC, you can get a fully qualified URL using the fourth parameter of Url.Action - protocol:
Url.Action("Index", "Home", null, Request.Url.Scheme)
I am stuck at an unexpected issue in my project. The issue is that there is a URL produced on the fly in my code that I have to submit it to a RESTful web service via a GET request. For e.g. the URL to submit looks like this: http://mysampleserver.com:8080/calc/8999/bpaX
The RESTful server accepts URL as its last parameter in the format below:
http://myRestfulAPI.domainname.com/capture/bbbb/http://mysampleserver.com:8080/calc/8999/bpaX
I also used System.Net.HttpUtility.UrlEncode(....) to encode the "URL to submit" first to incorporate it in the RESTful service call.
That resulted in getting the error below:
System.Web.HttpException: A potentially dangerous Request.Path value was detected from the client (:)
To try to resolve it, I followed the steps described per this web page but no luck.
I am using MVC 4 to implement the RESTful API in C#.
Any clue or idea how to get around this showstopper issue?
There are at least two solutions I can think of.
Change your RESTFul service to use post, because you send information to your server, and potentially it will change your resource status, based on HTTP protocol , you should use POST anyway.
You can also encode your url with Base64
The steps that you've tried are the correct steps. See also this question potentially dangerous... which is the same issue.
There are a number of characters that .NET doesn't allow in in a URL by default, and the : is one of them (as a query string, at least). They are 'potentially dangerous'. Making this change to the configuration file allows these characters to be passed through to your application.
You need to Url.Encode the url in the query string (mvc parameters) otherwise it is interpreted as more URL encoding for MVC to decode as parameters. Try something like #Url.Encode(yourStringObject) and pass it as the last value or as a query (i.e. &q=url)
are WCF service arguments automatically URI decoded or do I manually do it?
EDITED TO ADD:
When I originally put forward this answer I seem to remember having verified it in a test. Upon #JohnSaunders comment below I revisited with a new project. And found that a console app submitting the string above returned the string exactly as it had been submitted, without having URI encoded it. It may be that I did something unexpected previously. Anyway, #JohnSaunders is correct.
Original Incorrect Answer:
No, they are not, and yes you have to URI encode them if you're expecting special characters.
If you were to submit this to a WCF service in the stream of data passed to it:
"http://localhost/users/email/bill#microsoft.com"
It would show up on the server like this:
"bill"
The "#microsoft" gets removed
The passed data must be URI encoded to get it all to pass through.