I have a dynamic field defined on a request object like so:
/// <summary>
/// Some description of this field
/// <example>17.5 or MyString in some cases</example>
/// </summary>
public dynamic MyField { get; set; }
However, in our docs, the fields is showing up as:
myField object (Object) Nullable
Is there a way to add a more user-friendly description to this field using ReDoc? I'd at least like ReDoc to honor the example text. Note - this is in a dotnet core API using Swashbuckle.
Related
I have built a ASP.Net Core 3.1 Web API, and I am interested in auto-generating documentation for my clients. I've followed the very simple instructions found here: Microsoft docs to install either NSwag or Swashbuckle in my project and in both cases I can see documentation for my APIs, but there are no descriptions on the model properties.
Java seems to have an #ApiModelProperty annotation for this, but I don't see a similar attribute in .Net. Is it possible to add descriptions for Model properties in either of these swagger implementations, that would show up in Schema section in the Swagger UI?
Standard xml doc summary works for me.
public class TokenRefreshRequest
{
/// <summary>
/// The JWT access token
/// </summary>
public string AccessToken { get; set; }
/// <summary>
/// The refresh token that was sent via the new token endpoint or the last refresh.
/// </summary>
public string RefreshToken { get; set; }
}
Shows on the swagger page as;
You can add limited markdown as well but I can't seem to find the link.
I am using a TINYMCE text editor and I want to store text in database.
But when I tried to insert data in database using Insert query I found the error like "A potentially dangerous Request".Form value was detected from the client
TEXT THAT I HAVE WRITTEN IN EDITOR
for that i have used
EncodedString = System.Web.HttpUtility.HtmlEncode(Request.Form["txtcontent"]);
But it doesn't encode the tag..
First off, I'd recommend using a Model binding instead of Request.Form[].
For the actual question at hand, you'd want to add the AllowHtmlAttribute to the property which has the HTML.
Example View Model
public class MyViewModel
{
public string Name {get;set; }
[AllowHtml]
public string Content { get; set; }
}
In our ASP.NET MVC 4 application, one of the models has a field of the DateTime type. When editing such model objects via a form, the value for the DateTime field has to be non-empty and on the format yyyy-MM-dd H:mm:ss (e.g., 2012-10-17 10:49:00). How do I ensure this field is correctly validated in the application? I've tried the following annotations:
[System.ComponentModel.DataAnnotations.Required]
[System.ComponentModel.DataAnnotations.DisplayFormat(DataFormatString="yyyy-MM-dd H:mm:ss",
ApplyFormatInEditMode=true)]
However, validation of form data doesn't require all components of the format to be present. For instance, the value '2012-10-17' is accepted (leaving out the 'H:mm:ss' part). It's just verified that the field contains a valid DateTime string.
How should I ensure that this DateTime field is indeed on my specified format (yyyy-MM-dd H:mm:ss)?
Alternative solution - view-only model class
Darin's solution is of course valid, but it's not the only one you can use. And it would require you to write more complex code than with this solution that I'm going to show you here.
So this is an alternative. I'd suggest that instead of creating a custom model binder you rather create a separate view model class that instead of taking DateTime takes a string where you can set as complex validation regular expression as you like. And then have a method on it that would translate it to your application/domain model class instance (and back).
// suppose this app model
public class User
{
[Required]
public string Name { get; set; }
[Required]
public DateTime DateOfBirth { get; set; }
}
public class ViewUser
{
[Required]
public string Name { get; set; }
[Required]
[RegularExpression("\d{4}-\d{2}-\d{2}(?:\s\d{1,2}:\d{2}:\d{2})?")]
public string DateOfBirth { get; set; }
public ViewUser(User user)
{
this.Name = user.Name;
this.DateOfBirth = user.DateOfBirth.ToString("yyyy-MM-dd H:mm:ss");
}
public User ToPoco()
{
return new User {
Name = this.Name,
DateOfBirth = DateTime.Parse(this.DateOfBirth, "yyyy-MM-dd H:mm:ss")
};
}
}
With a bit of tweaking you could inherit ViewUser from User class and use new keyword on DateOfBirth and use base property to store correct typed value. In that case you wouldn't need the ToPoco method.
Note: you will have to use DateTime.TryParseExact method to parse your dates because they may include time or they may not. I didn't include that in my code because it depends on the exact requirements of your date input.
You could write a custom model binder which will use the exact format you have specified in the DisplayFormat attribute. I have shown an example of how this could be achieved in this post.
Also don't be confused into thinking that the DisplayFormat attribute overrides the Required attribute. The DisplayFormat is only used for displaying the field in the input field. It is not a validation attribute. It has strictly nothing to do with validation and when the form is POSTed to the server it is never used.
I am working with a ASP.NET MVC4 application. I have created a view model which contains menu items and I can switch languages in page by Resources file.
#region Properties
[Display(Name = "MenuText", ResourceType = typeof(App.App_Resources.Menu))]
public string menuText { get; set; }
public List<MenuItem> menuItems { get; set; }
#endregion
However, I want to get this resource string in my .cshtml file, then I try as following
#model App.Models.MenuViewModel
#Html.LabelFor(model => model.menuText) <- Success
#Html.DisplayForModel("menuText") <- Success
#Model.menuText <- Fail
I inserted a break point and found out that Model contains a property which name is menuText but value is null. And I checked that Html also contains a property Model and its menuText also is null.
However, menuItems has items since I assign objects in constructor.
Why the menuText cannot be initialized and assigned value to it?
Why I can succeed to show the resource string with first two but Model.menuText is null and fail to show anything? What is different between the models in #Html.XXX and #Model?
#Model.menuText retrieves the raw string value stored within the menuText property. Attributes are ignored.
Using LabelFor causes the Display attribute of the property to be examined. The localised string is stored in the attribute, not the property.
Note that I think the Model object/class should not be used to store information for display (that's what ViewData is for), but rather only for round-trip data that is sent from the client to the server.
I am looking for a text editor for my asp.net mvc project. I was able to find a lot of text editor out there works pretty well.
What I am looking for is a editor that accept only regualr character like "I am a superman".
I do not want to save "<p><strong>I am a superman</strong></p>" into my SQL table since I have to show it thru textbox(example : <%= Html.TextBox("Remark", Model.empExperience.Remark)%>).
Let me know.
Seeing as you do not wish to allow HTML, your best bet is to simply have a means of stripping HTML from the provided input. There's no need to implement a custom text editor for this sort of thing.
Have a look at: How can I strip HTML tags from a string in ASP.NET?
This is how you do it (to sum up the answers on the link Nathan provided):
private static readonly Regex StripHtml = new Regex("<[^>]*>", RegexOptions.Compiled);
/// <summary>
/// Strips all HTML tags from the specified string.
/// </summary>
/// <param name = "html">The string containing HTML</param>
/// <returns>A string without HTML tags</returns>
public static string StripHtmlTags(string html)
{
if (string.IsNullOrEmpty(html))
return string.Empty;
return StripHtml.Replace(html, string.Empty);
}