Thymeleaf not recognizing Strings just ModelAndView - thymeleaf

According to documentation like this I should be able to do this...
#RequestMapping("")
public String root(){
return "test";
}
However, this just returns the word "test". In order to get it to work I have to use the slightly more explicit...
#RequestMapping("")
public ModelAndView root(){
return new ModelAndView("test");
}
What little thing am I missing that is preventing the top version from rendering properly?

You should be using #Controller instead of #RestController.
#RestController is the equivalent of doing:
#RequestMapping("")
#ResponseBody
public String root(){
return "test";
}
Which is saying to return the result of the root() function as a json/xml object (which means just returning the string test). If you want "test" to represent the page to render, use #Controller.

Related

How to create GET method in API controller with parameters (e.g. sort query or search query)?

I have an API controller and GET method like this :
public IEnumerable<CountryDTO> GetContries(string sortOrder, string searchString)
{
return countryRepository.GetCos(sortOrder, searchString);
}
but when I try to get url api/countries
I`ve got an error like:
The requested resource does not support http method 'GET'.
How to fix this problem?
Try to explicitly mark the method [HttpGet] and make the following change to your method.
[HttpGet]
public IEnumerable GetCountries(string sortOrder, string searchString)
{
return countryRepository.GetCos(sortOrder, searchString).ToList();
}
Also, I would recommend before you make the change, simply test your method using a tool like SOAPUI or Fiddler. See if you are able to get a response. That way you know for sure that problem is not in the basics of the method formation.
[HttpGet]
public string GetCountries(string sortOrder, string searchString)
{
return ("returns list of countries");
}

Adding new parameter to a web API action method without disturbing the existing contract

I have an action method already written in my web api 2.0 project. I would like to add a new parameter without disturbing the existing contract. What is the best way to do that? Appreciate any best practice hints on this :)
Here's the code sample of what I intend to do:
Existing code:
[Route("{myId}",Name="MyId")]
Public IHttpActionResult Get(String myId)
{
//Some more code here
}
Url: http://localhost:8888/webapi/1111
Expecting to do something like the below:
//I want to keep the route name same for backwards compatibility.
[Route("{myId}/{myName}",Name="MyId")]
Public IHttpActionResult Get(String myId,string? myName)
{
//Some more code here
}
Url: http://localhost:8888/webapi/1111/John
The Url mentioned above hits the method rightly, but I never get the second parameter (myName) populated with John.
Thanks everyone for any help towards this.
Sree.
In your example you have myName as string? which is not allowed as:
The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'
A test controller was created to implement you action
[RoutePrefix("webapi")]
public class TestsController : ApiController {
[HttpGet]
[Route("{myId}/{myName}", Name = "MyId")]
public IHttpActionResult Get(string myId, string myName) {
//Some code to show the values of the parameters
return Ok(new { myId = myId, myName = myName });
}
}
When tested with webapi/1111/John the following response is returned
{"myId":"1111","myName":"John"}
which does include the value for MyName as John
If backwards uri webapi/1111 is tried, a NotFound response is returned as the template does not match the new action.
To fix this you need to make the myName parameter optional. To learn more about that check
Optional URI Parameters and Default Values
The new route will look like
//NOTICE THE `?` ON THE {myName} TEMPLATE
[Route("{myId}/{myName?}", Name = "MyId")]
public IHttpActionResult Get(string myId, string myName = null) {...}
You will notice that myName was made optional in the route {myId}/{myName?} and in the action parameter (string myId, string myName = null)
Now when tested with webapi/1111 the following response is returned
{"myId":"1111","myName":null}
Which would match your expected result for backwards compatibility.
String is a reference type so you don't need to make it nullable, it already is. Remove the '?' and remove the Name from the attribute. What happens then?

Web.Api deserialization fail for model parameter with different name

I've got one method, which take a model [AccountLinkRequest] as a parameter with url-encoded data. It's uses Json.NET by default, and also, I can't use the setting UseDataContractJsonSerializer = true cause I have generic output response model (in other methods)
[HttpPost]
public SomeResponse Link(AccountLinkRequest request)
{
if (request.CustomerId == null)
throw new Exception("Deserialization error here, pls help!");
// other actions
}
Here is my model class:
[DataContract]
[JsonObject]
public class AlertAccountLinkRequest
{
[DataMember(Name = "id")]
public string id { get; set; }
[DataMember(Name = "customer_id")]
[JsonProperty("customer_id")]
public string CustomerId { get; set; }
}
The problem: request.CustomerId is allways null. The request is pretty simple:
web_service_URL/link?customer_id=customer_id&id=id (url-encoded)
if I use Customer_Id instead of CustomerId, everything will be fine, but I'm on a jedy-way. Thank you!
There is not a simple answer how to achieve that. For more details please read this:
How to bind to custom objects in action signatures in MVC/WebAPI
Summary:
Manually call the parse function inside of your Action
Use a TypeConverter to make the complex type be simple
Use a custom model binder
So, if you for instance create your 'SmartBinder' which is able to consume some attributes, you can get what you want. Out fo the box there is no functionality for that, just the naming conventions...

Access the error message in ModelState error dictionary in ASP.net MVC unit test

I have added a key-value pair in the action result like this:
[HttpPost, Authorize]
public ActionResult ListFacilities(int countryid)
{
...
ModelState.AddModelError("Error","No facilities reported in this country!");
...
}
I have some cumbersome codes like these in a unit test to :
public void ShowFailforFacilities()
{
//bogus data
var facilities = controller.ListFacilities(1) as PartialViewResult;
Assert.AreSame("No facilities reported in this country!",
facilities.ViewData.ModelState["Error"].Errors.FirstOrDefault().ErrorMessage);
}
Of course, it works whenever I have only one error.
I don't like facilities.ViewData.ModelState["Error"].Errors.FirstOrDefault().ErrorMessage.
Is there an easier way for me to fetch the value from that dictionary?
Your FirstOrDefault isn't needed, because you'll get a NullReferenceException when accessing ErrorMessage. You can just use First().
Either way, I couldn't find any built-in solution. What I've done instead is create an extension method:
public static class ExtMethod
{
public static string GetErrorMessageForKey(this ModelStateDictionary dictionary, string key)
{
return dictionary[key].Errors.First().ErrorMessage;
}
}
Which works like this:
ModelState.GetErrorMessageForKey("error");
If you need better exception handling, or support for multiple errors, its easy to extend...
If you want this to be shorter you can create an extension method for the ViewData...
public static class ExtMethod
{
public static string GetModelStateError(this ViewDataDictionary viewData, string key)
{
return viewData.ModelState[key].Errors.First().ErrorMessage;
}
}
and usage:
ViewData.GetModelStateError("error");
Have you tried this?
// Note: In this example, "Error" is the name of your model property.
facilities.ViewData.ModelState["Error"].Value
facilities.ViewData.ModelState["Error"].Error

Pass a non static value to a actionfilter

Is ther any way to pass a non static value to a actionfilter parameter like below?
public class ProcuctController : Controller
{
private int userID = 1;
[TestFilter(x=userID)]
public ActionResult Index()
{
}
}
No, there isn't. Attributes are baked into the metadata of the resulting assembly and all values should be known at compile time. You can pass only constant values as attribute properties. And that's .NET limitation, not MVC.
depending on the situation, you could always pass in some kind of key to the filter, and then inside the filter you could do a lookup for the corresponding value in whichever datasource you prefer. this would allow you to get non-static data in your method, though it would be nice if it were as simple as your example :-)
You can't pass it in but you could do something like:
public class TestFilterAttribute : ActionFilterAttribute
{
public string UserId
{
get
{
return AppSettings["UserId"];
}
}
}

Resources