Comments (10)
Ok, @bufferUnderrun could you please give it a shot now?
Aslo @geoperez could you please create a few unit tests?
from embedio.
Hey, you are correct, and this is precisely the purpose of this project being public. I think I have a few ideas. Let me work on them
from embedio.
Just as a side note regarding the extension method to parse request: I admit I wrote this really quickly and I was surprised to see how low quality it was (my apologies). Hopefully this new version alleviates a lot of the problems some users may be having. I have obsoleted the methods currently in use.
from embedio.
@mariodivece i'm away from computer all this week-end... I could only test it on monday.
Before your fix, i was starting to use the same approach by replacing Dict<string,string> with Dict<string, object>(), so same idea ;)
IMO, the solution is not obvious because when using the new RequestFormDataDictionary() method, it implies parsing the value to check if it's a string or list. I'm wondering if there is a better way to do this...
Finally, for the rare use case (select/multiple), it seems to works.
from embedio.
I left the Dictionary of string, string methods available for users to still access. The difference is that it now converts arrays into NewLine-separated values of strings. While not ideal, it still works much better than the previous method. Furthermore, I need to find some time to support deep object parsing with something like a dynamic (i.e. Expando object) result. This way I could easily serialize into JSON and deserialize into a user-specified type.
from embedio.
Last post before week-end ;)
A few months ago, i wrote an UpdateModel()
Extensions which is very similar to the UpdateModel()
of the Asp.net MVC project. The basic idea was to simplify migration from my Asp.net MVC 5 projects to the new stack composed of embedio+riotjs. I've attach the snippet to this post.
The UpdateModel()
will map all postdata to an object properties. It takes optionnals parameters to include/exclude properties or force additionnals settings to the json method used for this mapping (Newtonsoft.Json).
Once the UpdateModel()
extension is loaded, this is how i use it :
/**
* User Controller (create/read/update/delete users)
**/
public class UserController : WebApiController {
// repositories (deal with sqlserver database)
private UserRepository _user = new UserRepository();
/**
* POST: create
**/
[WebApiHandler(HttpVerbs.Post, RestApi.RelativePath + "user/create")]
public bool Create(WebServer server, HttpListenerContext context) {
var user = new User(); // new empty instance of user class from user repository
try {
// map post date to user class instance
context.UpdateModel(user);
// save to db
_user.Add(user);
_user.Save();
// return to ajax caller
return context.JsonResponse(user);
}
catch (Exception ex) {
return context.JsonResponse(ex);
}
}
/**
* POST: user/update/{id}
**/
[WebApiHandler(HttpVerbs.Post, RestApi.RelativePath + "user/update/{id}")]
public bool Update(WebServer server, HttpListenerContext context, int id) {
// Retrieve existing user from User Repository
var user = _user.Get(id);
if (user == null) {
return NotFound(context);
}
try {
// updatemodel will populate user with post data except for usr_id and usr_login which i don't want to be changed
context.UpdateModel(user, null, new[] { "usr_id", "usr_login" }, new Newtonsoft.Json.JsonSerializerSettings() {
// force some settings to the json serialiser (ie french date format)
Culture = new System.Globalization.CultureInfo("fr-FR")
});
// save to db
_user.Save();
// return to ajax caller
return context.JsonResponse(user);
}
catch (Exception ex) {
return context.JsonResponse(ex);
}
}
} // end class
The UpdateModel()
support many arguments and can even map List<Object>()
. Suppose you have many users to add in the same post data :
<input type="text" name="user[0].usr_name" value="foo" />
<input type="text" name="user[0].usr_mail" value="[email protected]" />
<input type="text" name="user[1].usr_name" value="bar" />
<input type="text" name="user[1].usr_mail" value="[email protected]" />
var post = context.RequestFormData();
var users = new List<User>();
post.UpdateModel(users, "user"); // "user" is the input name prefix to all fields related to users in the html
// users then contains 2 User object map with the post data
from embedio.
@mariodivece I'm late but i've tested and, of course, your patch fix the issue. I will modify my extend method UpdateModel()
to handle the new type Dict<string, object>()
.
Thanks for your support
from embedio.
I really need to get deep object parsing right on this one. I will work on it as soon as I find some spare time
from embedio.
Example implementation
https://github.com/mariodivece/amcresthttp/blob/master/Amcrest.HttpApi/Models/Node.cs
from embedio.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from embedio.
Related Issues (20)
- Cannot control charset in ContentType header HOT 3
- How to send statuscode 202 HOT 1
- EmbedIO 3.5.0 NuGet package references wrong/old version of Unosquare.Swan.Lite HOT 22
- System.ObjectDisposedException when processing multiple requests at once HOT 2
- WithZipFile should open a read only handle(or have the option to) HOT 2
- FileModule or StaticFolderModule get stuck returning medium files HOT 4
- Add XML as response type
- EmbedIO Dual-Stack Localhost Listener Causes Empty Replies HOT 1
- How can I custom the Server header HOT 2
- Disable request logging only
- Cookies issue
- EmbedIO.Samples console app does not work when PublishAot is set to true HOT 2
- Web Server does not work in Xamarin.iOS (IOS-17) HOT 3
- How to report progess via api - event-stream
- HttpContext.OnClose not firing HOT 1
- Getting rid of double quotes around strings in a Web API response (turning off JSON serialization) HOT 1
- Issue Serving Static Folder Xamarin ( 3.5.2) Forms on Android
- Request.Url mixing up http and https
- server stop responding after some time HOT 4
- The return array is empty. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from embedio.