I'm working on a reply from email feature in my app, and I'm using mandrillapp.com. How it works now :
1. User replies from their email to something#something.com
2. Mandril receives the mail and sends the POST request to preconfigured endpoint of my service
3. I process that post request (create internal app message)
With regards to step 3, I have basically code that should create internal app message wrapped in the begin/rescue. So any potential errors would get reported to me, which they have in the past.
However now I've encountered a user who claims that the email has been sent (step 1), I contacted the mandrill support they say they have processed it and send to my endpoint (step 2).
So that only leaves me to see what happened in the step 3, within that specific time range. My app is on heroku, and my logs stored on AWS I could go back in past to that specific time and I could see a successful POST request from mandrill side.
What can I do to catch this case moving forward? And fix it of course, because I have no idea what is going on now.
I was thinking create a model named Mandrill hooks, which would have params column which would be a serialized hash and it would save all the params which came to my endpoint from mandrill.
Then I could check counts of webhooks received by my account and the ones sent by mandrill, and hopefully I spot 1 with different count, and since I will have the params data in my db, I'd be able to reproduce the case.
Or is there more wise approach, as this looks naive even to me?
I think there may be a wiser approach that enables you to have more control over everything and this sounds extremely similar to what I recently accomplished and I answered a similar question here. Does this help at all or is there some more specific help that you would like?
** Edit: Linked post/code below
I am running my Mandrill Webhook handling as an API project in VS and this is how I am have gotten it up and running:
[HttpPost]
public string Post()
{
/* Every Mandrill webhook uses the same general data format, regardless of the event type.
* The webhook request is a standard POST request with a single parameter (currently) - 'mandrill_events'. */
string validJson = HttpContext.Current.Request.Form["mandrill_events"].Replace("mandrill_events=", ""); //"mandrill_events=" is not valid JSON. If you take that out you should be able to parse it. //http://stackoverflow.com/questions/24521326/deserializing-mandrillapp-webhook-response
List<MandrillEvent> mandrillEventList = JsonConvert.DeserializeObject<List<MandrillEvent>>(validJson);
foreach (MandrillEvent mandrillEvent in mandrillEventList)
{
if (mandrillEvent.msg.email != null)
{
DataLayer.ReportingData.EmailSave(mandrillEvent); //Saves MandrillEvent email to database and sets a messageId for datalayer
}
}
foreach (MandrillEvent mandrillEvent in mandrillEventList)
{
DataLayer.ReportingData.MandrillEventSave(mandrillEvent); //Saves MandrillEvent object to database
}
return "DONE";
}
I then took the documented (and undocumented) JSON parameters of "mandrill_event" and used json2csharp.com to generate the C# properties. I created a class called "MandrillEvent.cs" and put these within:
public class SmtpEvent
{
public int ts { get; set; }
public DateTime SmtpTs { get; set; }
public string type { get; set; }
public string diag { get; set; }
public string source_ip { get; set; }
public string destination_ip { get; set; }
public int size { get; set; }
public int smtpId { get; set; } //added for datalayer
}
public class Msg
{
public int ts { get; set; }
public DateTime MsgTs { get; set; }
public string _id { get; set; }
public string state { get; set; }
public string subject { get; set; }
public string email { get; set; }
public List<object> tags { get; set; }
public List<object> opens { get; set; } //an array of containing an item for each time the message was opened. Each open includes the following keys: "ts", "ip", "location", "ua"
public List<object> clicks { get; set; } //an array containing an item for each click recorded for the message. Each item contains the following: "ts", "url"
public List<SmtpEvent> smtp_events { get; set; }
public List<object> resends { get; set; } //not currently documented on http://help.mandrill.com/entries/58303976-Message-Event-Webhook-format
public string _version { get; set; }
public string diag { get; set; } //for bounced and soft-bounced messages, provides the specific SMTP response code and bounce description, if any, received from the remote server
public int bgtools_code { get; set; } //Is it this? for bounced and soft-bounced messages, a short description of the bounce reason such as bad_mailbox or invalid_domain. (not currently documented but in JSON response)
public string sender { get; set; }
public object template { get; set; }
public string bounce_description { get; set; }
public Msg()
{
tags = new List<object>();
opens = new List<object>();
clicks = new List<object>();
smtp_events = new List<SmtpEvent>();
smtp_events.Add(new SmtpEvent());
resends = new List<object>();
}
}
public class MandrillEvent
{
public string #event { get; set; }
public string _id { get; set; }
public Msg msg { get; set; }
public int ts { get; set; }
public DateTime MandrillEventTs { get; set; }
public int messageId { get; set; } //added for datalayer
public List<string> SingleMandrillEventData { get; set; } //added for Reporting
public MandrillEvent()
{
SingleMandrillEventData = new List<string>();
msg = new Msg();
}
}
You now have your "mandrill_events" JSON object as a functioning C# object!! Did this help or do you need some more clarification/help?
Related
I am working on web app in ASP.NET MVC using Entity Framework. It is a Ticket Log tool for chats. So, if a chat comes in Agent will copy email, and Chat Date and Start time, click on SUBMIT. This will simply create an entry for chat in tool with following fields:
Email, ChatStartDateTime, ChatCreatedDateTime
So ChatStartDateTime will be different then ChatCreatedDateTime.
Now the Agent shares support on Chat, and may be he ends the chat after 10 mins. Now, he comes back to the web app and adds his comments for the action taken for chat on web app and clicks a button CHATCOMPLETE.
Clicking this button will update the other field properties like Description field and ChatEndDateTime.
So, now how can I get to know duration in between these fields
ChatStartDateTime and ChatStartDateTime
ChatStartDateTime and ChatEndDateTime
Chat model class:
public class Chat
{
[Key]
public int ChatId { get; set; }
[Required]
public string CustName { get; set; }
public string Query { get; set; }
public string Resolution { get; set; }
[Required]
public DateTime ChatStartDateTime { get; set; }
public DateTime? ChatCreateDateTime { get; set; }
public DateTime? ChatEndDateTime { get; set; }
public int Id { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
[ForeignKey("Id")]
public virtual User User { get; set; }
}
ChatLog controller action
public ActionResult MyChats()
{
using (Db db = new Db())
{
Chat dto = new Chat();
var uName = User.Identity.Name;
ViewBag.myTodayTicket = db.Chats.Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.ChatCreateDateTime) == DateTime.Today && x.Username == uName).Count();
return View(db.Chats.Where(x => x.Username == uName).ToArray().ToList());
}
}
Help is much appreciated.
Here is how I got it resolved #(string.Format("{0:hh\:mm\:ss}", item.ChatEndDateTime-item.ChatCreateDateTime)). Now Checking how can I tally all and get average for all Chat Logs
Is there a way in MVC to pass information from one controller to another? I have a character model that looks like this:
public class Character
{
[Key]
public string CharacterID { get; set; }
public string UserID { get; set; }
public string Name { get; set; }
public int Str { get; set; }
public int Con { get; set; }
public int Dex { get; set; }
public int Int { get; set; }
public int Wis { get; set; }
public int Cha { get; set; }
public int BaseAttack { get; set; }
}
And a separate weapon model like this:
public class Weapons
{
[Key]
public string WeaponID { get; set; }
public string UserID { get; set; }
public string CharacterID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Range { get; set; }
public int Damage { get; set; }
public int Crit { get; set; }
public int CritMultiplier { get; set; }
public string Hands { get; set; }
public string Distance { get; set; }
}
To create a weapon, you first need to create a character which assigned an ID, and I want to be able to pass that ID into the create method of my weapon controller. Is there a way to do this? Thanks
You can use TempData for this purpose. TempData stores data only between two requests. When you set the TempData the next request that is initiated can retrieve value from the TempData and it will be erased for any consequent requests.
[HttPost]
public ActionResult CreateCharacter()
{
// creates charaeters here and sets the tempdata
TempData['CharacterId'] = 50;
return RedirectToAction('CreateWeapon');
}
[HttpGet]
public ActionResult CreateWeapon()
{
var weaponModel = new WeaponModel() { CharacterId = (int)TempData['CharacterId'] };
return View(weaponModel);
}
and in your view simply have a hidden for the CharacterId, so it will be persisted if you your post fails validation or if you need to re-display the view.
#Html.HiddenFor(e => e.CharacterId);
Again this is just one approach, only if you you don't want to pass the CharacterId in the url.
You can also achive this just by passing it in the url:
[HttPost]
public ActionResult CreateCharacter()
{
// creates charaeters here and sets the tempdata
return RedirectToAction('CreateWeapon', new { characterId = 50 });
}
[HttpGet]
public ActionResult CreateWeapon(int characterId)
{
var weaponModel = new WeaponModel() { CharacterId = characterId };
return View(weaponModel);
}
I would be inclined to pass the character id to the create weapon action via routing, either as a route token that forms part of the path or via the query string. Be sure to check that the weapon can logically be associated with the character to whom the id corresponds.
You could also pass the id using TempData or Session, but considering both by default will take up memory on the web server, the simple option is to use the routing. In addition, unless you call TempData.Keep("key") after accessing TempData, the value will be removed from TempData after the first access, potentially causing issues if the user refreshes the browser window.
You could use RedirectToAction(), though as titled this will cause browser redirection.
return RedirectToAction("CreateWeapon", "Weapon", new { id = yourid });
or
#Html.ActionLink("CreateWeapon", "Create", new { id = yourid })
Edit: Your plain object property names and your action method variables need to match, to do this.
I'm using ASP.Net MVC 5 from Visual Studio. I want to create a user profile with complex types. I have modified the code of the User class in IdentityModels.cs file. Here's the code:
public class User : IUser
{
public User()
: this(String.Empty)
{
}
public User(string userName)
{
UserName = userName;
Id = Guid.NewGuid().ToString();
}
[Key]
public string Id { get; set; }
public string UserName { get; set; }
public string Phone { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public string Address { get; set; }
}
I've also changed the views for this model. This works great without any problems. However, if I change the type of the Address property above to Address, meaning: public Address Address { get; set; } it fails.
I've tried using the virtual keyword for it but it didn't work. Please note that every time I create the database tables from scratch. Also, I checked the database and the information is inserted into database with correct foreign keys but I don't know what the problem is.
The code execution fails in the code below in the line await Users.Create(user) which returns false:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
try
{
// Create a profile, password, and link the local login before signing in the user
User user = new User(model.UserName)
{
UserAddress = model.Address,
Email = model.Email,
Phone = model.Phone,
MobilePhone = model.MobilePhone
};
if (await Users.Create(user) &&
await Secrets.Create(new UserSecret(model.UserName, model.Password)) &&
await Logins.Add(new UserLogin(user.Id, IdentityConfig.LocalLoginProvider, model.UserName)))
{
await SignIn(user.Id, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError(String.Empty, "Failed to create login for: " + model.UserName);
}
}
catch (DbEntityValidationException e)
{
ModelState.AddModelError("", e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Update:
Here's the Address class in case you wonder:
public class Address
{
public int ID { get; set; }
[Required]
public string Country { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Street { get; set; }
[Required]
public string PostalCode { get; set; }
}
Update 2
Here's the image of the entered data:
It seems that this is a bug. I won't select this as the answer until it is absolutely proven. However when I change my controller's code from:
if (await Users.Create(user) &&
await Secrets.Create...
to:
await Users.Create(user);
if (await Secrets.Create...
it works without any problems. Seems to me that this should be a bug since I can load and edit the data perfectly.
This means that for some reason, even if the IUserStore.Create succeeds, it returns false in case the model is a complex type.
The problem that you have here (at least specific to changing Address to be an object rather than a string), is that your models aren't built correctly to relate them properly. Here is what you should be looking at.
public class User : IUser
{
public User()
: this(String.Empty)
{
}
public User(string userName)
{
UserName = userName;
Id = Guid.NewGuid().ToString();
}
[Key]
public string Id { get; set; }
public string UserName { get; set; }
public string Phone { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
// This FK doesn't need to explicitly be declared, but I do so as it helps me
// with the understanding of my structure a bit better.
public int AddressId { get; set; }
public Address Address { get; set; }
}
You also need to relate your Address back to your User class. I'm not sure how you want to do that, but, assuming that multiple people can live at the same address, you'll want a one-to-many relationship. (Right now, you receive an error because you don't specify the relationship.)
You have to do this in the Address model:
public class Address
{
[Key]
public int ID { get; set; }
[Required]
public string Country { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Street { get; set; }
[Required]
public string PostalCode { get; set; }
// I would give this a better property name than "Users" but just putting
// this here for now.
public virtual ICollection<User> Users { get; set; }
}
This way, when your database builds, Entity Framework can now properly build the relationships (where, before, it couldn't tell what you intended - hence the error when you switch over to Address).
Of course, there may be other issues, but, this is one that would cause problems.
This is related to this question, but in this case it's not something I am returning but rather the model binding. I am using Postmark to handle incoming emails, which posts to a page with a JSON payload.
I have a model as below and an action that takes in this JSON payload (posted with application/json) and processes it.
public class EmailModel
{
public IDictionary<string, string> Headers { get; set; }
public string From { get; set; }
public string Cc { get; set; }
public string HtmlBody { get; set; }
public string TextBody { get; set; }
public string ReplyTo { get; set; }
public string Tag { get; set; }
public string To { get; set; }
public string MessageID { get; set; }
public string MailboxHash { get; set; }
public string Subject { get; set; }
public List<Attachment> Attachments { get; set; }
}
public class Attachment
{
public string Content { get; set; }
public int ContentLength { get; set; }
public string ContentType { get; set; }
public string Name { get; set; }
}
This works fine for small attachments, but for anything that exceeds the default maxJsonLength property causes an error in deserialization. ("Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.") Because I want to accept image attachments, this means most images fail.
I've tried updating the web.config, but as per those other threads, this doesn't help for MVC controllers. I figure I can probably do what was mentioned in a custom IModelBinder, but I'm struggling with how to intercept the deserialization. (In other words, it still fails because the deserialization has happened already).
Any suggestions? I'm sure it's just something stupid that I'm missing....
You could write a custom JsonValueProviderFactory that uses Json.NET:
public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
var bodyText = reader.ReadToEnd();
return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
}
}
and in your Application_Start:
ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
I am developing a application for Sales Order Management using ASP.NET MVC 3.0. I need to develop a page where Customer Details can be added.
Customer Details Include
public class Customer
{
public int ID { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Alias { get; set; }
public int DefaultCreditPeriod { get; set; }
public Accounts Accounts { get; set; }
public IList<Address> Addresses { get; set; }
public IList<Contact> Contacts { get; set; }
}
public class Accounts
{
public int ID { get; set; }
public string VATNo { get; set; }
public string CSTNo { get; set; }
public string PANNo { get; set; }
public string TANNo { get; set; }
public string ECCNo { get; set; }
public string ExciseNo { get; set; }
public string ServiceTaxNo { get; set; }
public bool IsServiceTaxApplicable { get; set; }
public bool IsTDSDeductable { get; set; }
public bool IsTCSApplicable { get; set; }
}
public class Address
{
public int ID { get; set; }
public AddressType Type { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string Line4 { get; set; }
public string Country { get; set; }
public string PostCode { get; set; }
}
public class Contact
{
public int ID { get; set; }
public ContactType Type { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string PhoneNumber { get; set; }
public string Extension { get; set; }
public string MobileNumber { get; set; }
public string EmailId { get; set; }
public string FaxNumber { get; set; }
public string Website { get; set; }
}
Customer Requires a single page to fill all the customer details(General info, Account Info,Address Info and Contact Info). There will be multiple Addresses(Billing, Shipping, etc) and multiple Contacts (Sales, Purchase). I am new to MVC. How to Create the View for the above and Add multiple Address dynamically?
I often create wrapper models to handle this kind of situation e.g.
public class CustomerWrapperModel
{
public Customer Customer { get; set;}
public Accounts Accounts { get; set;}
public List<Address> AddressList { get; set}
//Add
public CustomerWrapperModel()
{
}
//Add/Edit
public CustomerWrapperModel(Customer customer, Accounts accounts, List<Address> addressList)
{
this.Customer = customer;
this.Accounts = accounts;
this.AddressList = addressList;
}
}
then declare the View to be of type CustomerWrapperModel and use editors like so:
#model MyNamespace.CustomerWrapperModel
#Html.EditorFor(model => model.Customer)
#Html.EditorFor(model => model.Accounts)
#Html.EditorFor(model => model.AddressList)
and have a controller to receive the post that looks like this:
[HttpPost]
public ActionResult(Customer customer, Accounts accounts, List<Address> addressList)
{
//Handle db stuff here
}
As far as adding addresses dynamically I found the best way to do this if you're using MVC validation and want to keep the list structured correctly with the right list indexes so that you can have the List parameter in your controller is to post the current Addresses to a helper controller like this:
[HttpPost]
public PartialResult AddAddress(List<Address> addressList)
{
addressList.Add(new Address);
return PartialView(addressList);
}
then have a partial view that just renders out the address fields again:
#model List<MyNamespace.Address>
#{
//Hack to get validation on form fields
ViewContext.FormContext = new FormContext();
}
#Html.EditorForModel()
make sure you address fields are all in one container and then you can just overwrite the existing ones with the returned data and your new address fields will be appended at the bottom. Once you have updated your container you can do something like this to rewire the validation:
var data = $("form").serialize();
$.post("/Customer/AddAddress", data, function (data) {
$("#address-container").html(data);
$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("form");
});
NB. I know some people with have an issue with doing it this way as it requires a server side hit to add fields to a page that could easily just be added client side (I always used to do it all client side but tried it once with this method and have never gone back). The reason I do it this way is because it's the easiest way to keep the indexes on the list items correct especially if you have inserts as well as add and your objects have a lot of properties. Also, by using the partial view to render the data you can ensure that the validation is generated on the new fields for you out of the box instead of having to hand carve the validation for the newly added client side fields. The trade off is in most cases a minor amount of data being transferred during the ajax request.
You may also choose to be more refined with the fields you send to the AddAddress controller, as you can see I just post the entire form to the controller and ignore everything but the Address fields, I am using fast servers and the additional (minor) overhead of the unwanted form fields is negligible compared to the time I could waste coding this type of functionality in a more bandwidth efficient manner.
You pass your root model object to the View call in your controller like this:
public ActionResult Index() {
var customer = GetCustomer(); // returns a Customer
return View(customer);
}
And then your view looks something like this:
#model Customer
<!DOCTYPE html>
<!-- etc., etc. -->
<h1>Customer #Model.Name</h1>
<ul>
#foreach (var address in Model.Addresses) {
<li>#address.Line1</li>
}
</ul>
One gets the picture.
The code above depends on the #model directive, which is new in ASP.NET MVC 3 (see this blog post).
Is a good question :D for normal navigation properties such as Accounts doing this is not to hard:
#Html.EditorFor(model => model.Accounts.ID)
#Html.EditorFor(model => model.Accounts.VATNo)
will do something you want. But for collection navigation properties (Addresses and Contacts) you can't do this in one place by default. I suggest you use a different page for Addresses (and one for Contacts). Because it is the easiest way. But if you want to do this in one place (and also with out AJAX requests), you can create view by Customer, use scaffolding for model and it's simple navigation properties, and for lists (Addresses, Contacts) you must add them with JavaScript to the input fields (for example for each Address added, put it in an Array) and post fields to server. At server you can get main model and simple properties by default model-binder and for lists, you can 1) create your own model binder 2) parse them from inputted strings by yourself. Good lock