Asp.Net Mvc Consume Url posting json data - web-services

I was wondering if anyone had any pointers for parsing json data consumed from a URL in Asp.Net. I've found plenty of docs about Model Binding json datatypes but this is coming from a URL and I cant seem to find an example for that. The closest thing I've found is datacontractjsonserializer but again, I cant seem to find an example of that in context with a URL outputting the json data. Any help is appreciated.

You could use the JavaScriptSerializer class. You start by defining a model class which will hold the data. So let's suppose that the remote URL returns the following JSON:
{ name: 'John', addresses: [ { city: 'Paris' }, { city: 'London' } ] }
which could be represented by this model:
public class Person
{
public string Name { get; set; }
public Address[] Addresses { get; set; }
}
public class Address
{
public string City { get; set; }
}
And then deserialize the received JSON back to the model:
var serializer = new JavaScriptSerializer();
// TODO: Fetch the JSON from a remote URL
var json = "{name: 'foo', addresses: [{city: 'Paris'}, {city: 'London'}]}";
var person = serializer.Deserialize<Person>(json);
UPDATE:
In order to fetch the JSON from remote url you could use WebClient:
using (var client = new WebClient())
{
string json = client.DownloadString("http://someurl.com");
}

Here is what I have so far. A product of all answers that I get here in stack.
The idea is to get the json value from external web service and publish it in my controller as a json values and I dont have to create model for it. Hope this helps.
public class ApiJson: Controller
{
public JsonResult getUser()
{
WebClient client = WebClient();
NameValueCollection data = new NameValueCollection();
data.Add("param1", "value1");
byte[] result = client.UploadValues("http://localhost:9000/", data);
String json = Encoding.ASCII.GetString(result);
JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic item = serializer.Deserialize<object>(json);
return Json(item, JsonRequestBehavior.AllowGet);
}
}

Related

Why Glass Mapper Returning Null Values?

I'm using Glass V4. I have a set up of MVC Web Area Project.
I have installed the Glass Mapper in the Main Project (WebProject).
I'm trying to do the Glass Casting in my Area Project.
public class ContactController : SitecoreController
{
private readonly ISitecoreContext _context;
private IGlassHtml _glassHtml;
public ContactController()
: this(new SitecoreContext())
{
}
public ContactController(ISitecoreContext context)
{
_context = context;
_glassHtml = new GlassHtml(context);
}
// GET: Contact
public ActionResult ContactUs()
{
var db = Sitecore.Context.Database;
var datasource = db.GetItem(RenderingContext.Current.Rendering.DataSource);
var ViewModel = new Models.ContactUs();
ViewModel.Headerstring = datasource.Fields["Headerstring"].Value;
ViewModel.Substring = datasource.Fields["Substring"].Value;
ViewModel.Description = ((MultilistField)datasource.Fields["Description"]).GetItems().Select(s => s.Fields["Line"].Value).ToList<string>();
return View(ViewModel);
}
public ActionResult ContactUsGlass()
{
var model = _context.GetCurrentItem<ContactUsGlassModel>();
return View(model);
}
}
I'm able to get the value with the First Action Method but not with the second.
Model:
public class ContactUs
{
public string Headerstring { get; set; }
public string Substring { get; set; }
public List<string> Description { get; set; }
}
Glass Model:
public class ContactUsGlassModel
{
public virtual string Headerstring { get; set; }
public virtual string Substring { get; set; }
}
I understand I don't need to register my Namespace in Glass V4.
You should not use _context.GetCurrentItem method. Use _context.GetItem instead:
public ActionResult ContactUsGlass()
{
var model = context.GetItem<ContactUsGlassModel>(RenderingContext.Current.Rendering.DataSource);
return View(model);
}
You don't want to get model from your Sitecore.Context.Item (which is used in GetCurrentItem method. You want to get your model from the DataSource of the current rendering.
What #Marek has answered is the right way of pulling the rendering item into model. GetCurrentItem by default gives the page item being served by Sitecore. If the fields that your model needs are fields of your page item then GetCurrentItem can also fill your model. If Datasource nesting is enabled, then if the datasource is not set on the rendering, Sitecore returns the page item again.
You can inherit from GlassController and then use GetLayoutItem() to get the datasorced item. If it's null then you need to publish the template in sitecore and make sure you mappings are correct if you are not using TDS :)

Extract mail list addresses from MailGun

I want to extract the addresses OUT OF MailGun, into a CSV and delete the MailGun altogether.
The MailGun database is the only copy of the 951 addresses I have absolutely no access to the database in any form other than looking at the list in MailGun.
http://documentation.mailgun.com/api-mailinglists.html#mailing-lists
This is my solution to this problem in C# ,and it can get all members of a list(not only 100 limited).In addition, i use Newtonsoft.Json. Update: I found the 'total_count' has a maximum value 10000, so if the members in your mailing list is more than 10000, the request will return the maximum value 10000! In that case, this is not a great solution!
public void ExportMailList(string listName)
{
RestClient client = new RestClient();
List<MemberDetail> totalMember = new List<MemberDetail>();
client.BaseUrl = new Uri("https://api.mailgun.net/v3");
client.Authenticator =
new HttpBasicAuthenticator("api",
"key-yourKey");
RestRequest reqForTotal = new RestRequest();
reqForTotal.Resource = "lists/{list}/members";
reqForTotal.AddParameter("list", listName, ParameterType.UrlSegment);
int resultTotal= JsonConvert.DeserializeObject<Member>(client.Execute(reqForTotal).Content).total_count;
int skipTimes = resultTotal / 100;
for (int i = 0; i <= skipTimes; i++)
{
RestRequest request = new RestRequest();
request.Resource = "lists/{list}/members";
request.AddParameter("list", listName, ParameterType.UrlSegment);
request.AddParameter("skip",100*i);
totalMember.AddRange(JsonConvert.DeserializeObject<Member>(client.Execute(request).Content).items);
}
//CreateCSVFromGenericList(...);
}
public class Member
{
public List<MemberDetail> items { get; set; }
public int total_count { get; set; }
}
public class MemberDetail
{
public string address { get; set; }
public string name { get; set; }
public bool subscribed { get; set; }
public object vars { get; set; }
}
In the method CreateCSVFromGenericList(..), you can refer to this blog, and export the data to .csv file in any format you want.
Using curl,php or any other programming language you can achieve this, then simply delete the mailing list through the control panel:
curl -s --user 'api:YOURAPIKEY' -G \
https://api.mailgun.net/v2/lists/Your#MailingListName.com/members
Apikey and mailing list name are available from the control panel!
In python :
def get_members():
return requests.get(
"https://api.mailgun.net/v3/lists/LIST_NAME/members",
auth=("api", "key-YOUR_KEY"),
data={'limit': 100, 'skip': 1380})
Note that limit and skip are useful to paginate through your member list.

Email Validation MVC4 avoiding mail sites

Im using VS2012, ASP MVC4.
I want validate the new registered user email using this on my model:
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
This works, but I want block some email sites how: 10minutemail, etc.. I want searching a option that allows me config the DataType Email Anotation or extends it..
I thinks this can be more clean that a big regex that validate all.
Thanks a lot
You can create a custom email validation attribute that wraps over EmailAddressAttribute you are using now:
public class CustomEmailValidationAttribute : ValidationAttribute
{
private string[] blockedProviders = new[]
{
"10minutemail.com",
"some-temporary-email.net"
};
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
var emailValidationAttribute = new EmailAddressAttribute();
if (!emailValidationAttribute.IsValid(value))
return new ValidationResult("Invalid email");
bool isBlocked = blockedProviders.Any(pr => ((string)value)
.EndsWith(pr, StringComparison.InvariantCultureIgnoreCase));
if (isBlocked)
return new ValidationResult("Email provider is not allowed");
return ValidationResult.Success;
}
}
Then you can mark email fields with [CustomEmailValidation] instead of [EmailAddress].

How to convert jersey/jackson list to String in response

I have a List type field populated using solrj where it marshals data directly to bean using getBean() method. The solr field is marked as multivalued but it really is single valued. In the rest response I want to transmit it as a single string. Here is the code
#XmlRootElement
#JsonSerialize(include = Inclusion.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class Record {
#JsonIgnore
#Field //solrj field populated based on schema type
private List<String> titleList;
public String getTitle() {
if(titleList!= null && titleList.size() > 0) {
return titleList.get(0);
}
return "";
}
}
When I get the response object from non jersey rest clients I see the 'title' field populated correctly as String but with jersey REST client I get it as empty String. How can it be correctly deserialized as derived value for all REST clients?
I am getting value from java client as
Record response = target.queryParams(queryParams).request().buildGet().invoke(Record.class);
Chrome Rest client output
{
"title": "new trend",
jersey client output
{
"title" : "",
I used #JsonIgnore on both getter and setter methods instead of field. That worked for both deserialization and serialization
#Field("title")
private List<String> titleList;
#JsonIgnore
public List<String> getTitleList() {
return titleList;
}
#JsonIgnore
public void setTitleList(List<String> titleList) {
this.titleList= titleList;
}
public String getTitle() {
if(titleList!= null && titleList.size() > 0) {
return titleList.get(0);
}
return null;
}

Custom Data-annotation not firing in unit-test

I am currently experimenting with validation attributes,
and now I am trying to validate my ViewModel which contains an EmailAddress with a custom validation attribute.
public class UserLoginModel
{
[Required]
[EmailAddress]
public string email { get; set; }
[Required]
public string password { get; set; }
public bool rememberMe { get; set; }
}
I have made a unit-test where I give a false email address and try to validate my viewmodel.
[TestMethod]
public void TestingInvalidEmailAddress()
{
UserLoginModel model = new UserLoginModel();
model = GetAValidLoginModel(); //Get a default-model where all parameters are correct
model.email = "thisisnotavalidemail.com";
ValidationContext context = new ValidationContext(model, null, null);
var results = new List<ValidationResult>();
bool validModel= Validator.TryValidateObject(model, context, results);
//This is always true
Assert.IsFalse(validModel);
}
The result of this test is always False.
So I checked my attribute, because I thought I might have made a mistake:
[TestMethod]
public void Email()
{
string email;
var attr = new EmailAddressAttribute();
email = "myemail#domain.com";
Assert.IsTrue(attr.IsValid(email));
email = "thisisnotavalidemail.com";
Assert.IsFalse(attr.IsValid(email)); //If this fails, the test is successfull
}
And that did pass the test, using the exact same email address.
And when I test it in my browser, it also validates correctly.
So why does it not tell me that my email address is invalid in the first test-method?
I found my solution in over here.
Apparently I am just missing an extra parameter.
bool validModel= Validator.TryValidateObject(model, context, results, **true**);