Access to raw query parameters in ASP.NET Core - query-string

I want to access to raw query parameters in ASP.NET Core 2.0
Currently HttpRequest.Query provides access to strings decoded from url encoded encoding, but it decodes it incorrectly.
How can I access to raw parameters instead of parsing full query string by hands?

Ok, it seems I overcome this issue.
So, how I did it?
I used QueryFeature class from asp.net core source and replaced call to QueryHelpers.ParseNullableQuery (you can find it here) with slightly changed implementation of this helper without call to Uri.UnescapeDataString to parameter value.
Then created middleware class that sets my custom QueryFeature for HttpContext:
context.Features.Set<IQueryFeature>(new MyQueryFeature(context.Features));
and used it during app configure.
Don't forget that after this all your parameters will stay url encoded if they were.

Related

How OData works in AspNet MVC without change any code

To enable OData after installing the Microsoft ASP.NET Web API 2.2 package, the only thing to do is add the attribute EnableQuery and return IQueryable.
If the attribute EnableQuery is only metadata, what change in the framework?
I mean, when an request arrive the framework matches the url with the Route Table and then create the controller to manage the request. What does change with OData?
EnableQueryAttribute derives from ActionFilterAttribute, which means it can affect the result of an action via its OnActionExecuted method (called internally by Web API). Take a look at the source code to see what EnableQuery is really doing.

Best way to upload files with Restangular + Api-platform (Symfony2)

I use api-platform for the first time and I am looking for the best way to upload files.
My situation :
An entity "Post" with a OneToMany relation "Media" (SonataMedia) on server side, I'm working on the create view (Angular + Restangular).
All fields (except file type fields) are functional (persist OK). Now, for the file type fields, what do I have to do?
Asynchronous upload? in this case how to link files upload with my entity (which is not persisted yet)
upload files on form entity submit?
In every case, what it needs to send to server? How Api-platform manages it?
There is no builtin file upload support as time of writing in API Platform. You need to write your own code to handle uploads.
However, some work is some work being done to natively add this feature and you can already use it.
Basically, the idea is:
Encode the file to upload as data: URI client-side (using JavaScript)
Send this data URI (basically a base64 string) as a typical string entity property using Restangular (or any other HTTP client)
Decode the data: URI server-side to a regular file using the Symfony Serializer
Store this regular file on the server: it's up to you to store it on the filesystem or in more advanced backends such as Mongo GridFS or Amazon S3
Client-side
To get a data: URI client-side from a selected file, you can rely on the readAsDataURL of the FileReader JavaScript API: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL.
Send the resulting string as usual using Restangular.
Server-side
The API Platform normalization system is built on top of the Symfony Serializer. I've contributed a data: URI normalizer and denormalizer in Symfony: https://github.com/symfony/symfony/pull/16164
It is not merged yet but you can copy it in your own project during the meantime.
Register the normalizer:
services:
data_uri_normalizer:
class: 'AppBundle\Serializer\DataUriNormalizer'
tags:
- { name: serializer.normalizer }
Then create (and register) a new normalizer decorating the ItemNormalizer of API Platform to transform your property containing the file encoded as a data: URI to a standard \SplFileObject using the DataUriNormalizer::denormalize() method.
In a future version of API Platform (planned for the v2.1 with Symfony 3.1) all this logic will be automatically available and registered.

How to write a webscript to returns the ticket for the current user?

I need to implement a web script that generates a returns the ticket for the current user. This web script is addressed by the URI I use to setup a URLConncetion. The ticket should be contained in the response body, which I need to evaluate in my JSP (or Java code) to extract the ticket. How can be done by a simple JavaScript / FreeMarker web script, using the JavaScript session root scope object to retrieve the ticket, i.e. session.getTicket() ?
Can any one pls write the steps to do?
ticket.get.html.ftl (or json or whatever you want):
${session.ticket}

Setting / Editting the current FacebookWebContext AccessToken

I'm coding an ASP.NET website that works with facebook.
To make my own life easier, I've decided to work with the facebook C# SDK.
At this point, I've already obtained a valid access token, thus I've authorised correctly.
(I have done this through server-side C# code, not via the JavaScript SDK)
I now want to store this access token in the current FacebookWebContext, so that I can use it through the whole website.
However, when I try to write the value in FacebookWebContext.Current.AccessToken, I get the following error:
Property or indexer FacebookWebContext.Current.AccessToken cannot be assigned to -- it is read only
Now, I could try to create a new FacebookWebContext, and set this one as the current one, but the constructor doesn't accept an access token.
How do I put my access token in the current facebook web context?
It should already have the value when you do : FacebookWebContext.Current.AccessToken
Is it empty?

Posting base64 encoded files to an asp.net 1.1 page

We are making an automated patching application and would like to post files on production server through asp.net page (or maybe a web service), since we can only access production server via http. The page should accept files and store them to appropriate location. The path to files will be declared in external XML file.
So, is it possible posting a base64 encoded files within body tag and HOW? maybe even any better approach?
If you plan to use Base64 encoding.
Take a look at
System.Convert.ToBase64String()
System.Convert.FromBase64String()
System.Convert.ToBase64CharArray()
System.Convert.FromBase64CharArray()
See Using XML CDATA nodes to send files via a Web Service
why not create a webservice which accepts an object like:
class postfile
{
public byte[] fileByte;
public string fileName;
}
Then add a web reference in your client app.
.net will serialize the object for you.
You will need to secure this using wse security and might require the service use impersonation to write the file on the server.

Resources