Hi I'm learning Nancy and I'm trying to bind to a model, but I'm getting the error:
Error 8 'NancyFxTutorial.CarModule' does not contain a definition for 'Bind' and no extension method 'Bind' accepting a first argument of type 'NancyFxTutorial.CarModule' could be found (are you missing a using directive or an assembly reference?) C:\Development\Projects\C#\Web\Nancy\NancyFxTutorial\NancyFxTutorial\CarModule.cs
model:
public class BrowseCarQuery
{
public string Make { get; set; }
public string Model { get; set; }
}
public class CarModule : NancyModule
{
public CarModule()
{
Get["/status"] = _ => "Hello World";
Get["/Car/{id}"] = parameters =>
{
int id = parameters.id;
return Negotiate.WithStatusCode(HttpStatusCode.OK).WithModel(id);
};
Get["/{make}/{model}"] = parameters =>
{
BrowseCarQuery model = new BrowseCarQuery();
var carQuery = this.Bind<>()
};
}
}
any clues?
Thanks in advance
The Nancy model binding methods are defined as extension methods on the NancyModule class.
And these extension methods can be found in the Nancy.ModelBinding namespace.
So you need to using the Nancy.ModelBinding namespace to access the Bind() and BindTo() methods.
So add this line to your source file:
using Nancy.ModelBinding;
Related
I have this controller in my asp.net-core web-api project:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public ActionResult<List<ActualModel>> Get()
{
return new List<ActualModel>()
{
new ActualModel { StringValue1 = "SomeString" },
new ActualModel { StringValue1 = "MoreString" }
};
}
}
The only method in this controller returns a list of "ActualModel". The definition of it looks like this:
public class ActualModel
{
public string StringValue1 { get; set; }
}
And my swashbuckle generated Swagger-UI shows it like this:
But I would like the Swashbuckle to generate the Swagger-Specification so that it not contains the "ActualModel" but the following model instead:
public class OtherModel
{
public string StringValue2 { get; set; }
public int IntValue1 { get; set; }
}
I've noticed there is a function that I think I can use for this:
services.AddSwaggerGen(c =>
{
c.MapType<ActualModel>(() => new OpenApiSchema { Type = "string" });
But I can't figure out how I get the "OtherModel" to be generated into an OpenApiSchema so that it replaces the "ActualModel" nor can I find information or examples about it.
I tried giving it the full class name with namespace and without the namespace, I googled for "Swashbuckle replace Type with other Type" and "Swashbuckle MapType" but I cant find any examples which show how to simply replace one type with another type that is defined in the application.
Background: The above is just an example of course. In a "real" project I have generic and hierarchical class-definitions that I want to return in my controller but I have a Serializer in between the value the controller returns and the actual network output which changes the data structure to another, more simple structure for use on a Typescript side. That's why I want to replace the swashbuckle generated model definitions.
Well, you could just tell swagger what you are producing if for example it's not obvious because of templates or a hierarchy:
[HttpGet]
[ProducesResponseType(typeof(OtherModel[]), 200)] // <--- This line
public ActionResult<List<ActualModel>> Get()
{
return new List<ActualModel>()
{
new ActualModel { StringValue1 = "SomeString" },
new ActualModel { StringValue1 = "MoreString" }
};
}
However, it's your job to make sure they are actually compatible, there are no checks.
I have an MVC attribute set up like this:
public class Activity : FilterAttribute
{
public string[] Tags { get; set; }
public Directive Directive { get; set; }
}
and a corresponding filter class
public class ActivityFilter : IActionFilter
{
public ActivityFilter(IService service, IEnumerable<string> tags, Directive directive)
{
...
}
}
My bindings for the attribute to filter injection look like this:
this.BindFilter<ActivityFilter>(FilterScope.Action, 0)
.WhenActionMethodHas<Activity>()
.WithPropertyValueFromActionAttribute<Activity>("tags", a => a.Tags)
.WithPropertyValueFromActionAttribute<Activity>("directive", a => a.Directive);
When I try to debug my web site, I get an error from Ninject stating that it can't activate the ActivityFilter because no matching bindings are available for int, specifically:
ActivationException: Error activating int
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency int into parameter directive of constructor of type ActivityFilter
1) Request for ActivityFilter
It seems like Ninject is misinterpreting the enumeration parameter for an integer, but I'm at a loss as to how to resolve this problem.
The problem ended up being the method I was using for binding:
this.BindFilter<ActivityFilter>(FilterScope.Action, 0)
.WhenActionMethodHas<Activity>()
.WithPropertyValueFromActionAttribute<Activity>("tags", a => a.Tags)
.WithPropertyValueFromActionAttribute<Activity>("directive", a => a.Directive);
I should have used WithConstructorArgumentFromActionAttribute since I was trying to inject into a constructor argument (I mistakenly thought that the Constructor/Property referred to the source of the value, not the destination.)
Once I changed this it worked fine.
I created the following:
public class HttpStatusErrors
{
public HttpStatusErrors()
{
this.Details = new List<HttpStatusErrorDetails>();
}
public string Header { set; get; }
public IList<HttpStatusErrorDetails> Details { set; get; }
}
public class HttpStatusErrorDetails
{
public HttpStatusErrorDetails()
{
this.Errors = new List<string>();
}
public string Error { set; get; }
public IList<string> Errors { set; get; }
}
In my code I am using it like this:
var msg = new HttpStatusErrors();
msg.Header = "Validation Error";
foreach (var eve in ex.EntityValidationErrors) {
msg.Details. // Valid so far
msg.Details.Error // Gives the error below:
The Ide recognizes msg.Details as being valid but when I try to write the second line I get:
Error 3 'System.Collections.Generic.IList<TestDb.Models.Http.HttpStatusErrorDetails>'
does not contain a definition for 'Error' and no extension method 'Error' accepting a first
argument of type 'System.Collections.Generic.IList<TestDb.Models.Http.HttpStatusErrorDetails>'
could be found (are you missing a using directive or an assembly reference?)
C:\K\ST136 Aug 16\WebUx\Controllers\ProblemController.cs 121 33 WebUx
Is there something I am doing wrong? I thought the way I had this set up that new Lists would be created when the first class was created.
msg.Details returns a List object. List<T> does not have an Errors property. You need to access a specific element in your list, and only then will you have your Errors property.
For example:
msg.Details[0].Error
In your code you might want to make sure that msg.Details contains elements before trying to access them, or better yet iterate over them in a foreach loop.
Details is a collection. The property Error belongs to an item in the collection Details. There is no property Error on that collection Details
you are tying to go to Details which is a IList<HttpStatusErrorDetails>.
if you want to go through the items in that list u need go over them
for example
msg.Details[number].Errors
or
foreach(HttpStatusErrorDetails err in msg.Details)
{
err.Errors
}
I really can't understand what the problem is here and why I'm getting this error
public ActionResult Index()
{
var V_Model = new V_ViewModel() { TagName=V_Model.TagName};
return View(V_Model);
}
public class V_ViewModel
{
public string TagName { get; set; }
}
The Error:
is a 'type' but is used like a 'variable'
Isn't it the way to use View Model in MVC?
I assume you have another class called V_Model. The compiler is then telling you that the type V_Model is being used as a variable:
Also, if you do not have a class called V_Model you are attempting to create a new instance of V_ViewModel called V_Model and assign it's TagName to a variable that has not been created.
Rename V_Model to something else and remove TagName = V_Model.TagName
var testModel = new V_ViewModel() { TagName = "TestTag" };
Hi I have a class in folder App_Code/BLL/Countries.cs ,which is a public class.
But when I tried to access the class in a ASP.NET page like:
Countries MyCountries = new Countries();
its showing error:
"The type or namespace name 'Countries' could not be found (are you missing a using directive or an assembly reference?)"
Ok your countries class will look something like this
namespace BGridNDynamicPage1
{
public class Countries()
{
......
}
}
and your code above will be something like this:
using System.Web;
etc.
namespace BGridNDynamicPage
{
public class something() : Page
{
Countries MyCountries = new Countries();
}
}
change your second class too:
using BGridNDynamicPage1;
using System.Web;
etc.
namespace BGridNDynamicPage
{
public class something() : Page
{
Countries MyCountries = new Countries();
}
}
See http://msdn.microsoft.com/en-us/library/sf0df423.aspx