T4MVC Bug In generated code (Identifier expected) - t4mvc

I have just installed T4MVC in a new project and I am having the following error:
(I have used T4MVC in previous projects and never had this issue)
CS1001: Identifier expected
The error come from this generated code (in file T4MVC.cs):
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
internal partial class T4MVC_System_Web_Mvc_FileResult : System.Web.Mvc.FileResult, IT4MVCActionResult
{
public T4MVC_System_Web_Mvc_FileResult(string area, string controller, string action, string protocol = null): base(" ")
{
this.InitMVCT4Result(area, controller, action, protocol);
}
protected override void WriteFile(System.Web.HttpResponseBase ) { }
public string Controller { get; set; }
public string Action { get; set; }
public string Protocol { get; set; }
public RouteValueDictionary RouteValueDictionary { get; set; }
}
The method WriteFile is missing the identifier for System.Web.HttpResponseBase paremeter.
Someone having the same issue?
There is a fix available?

This is a known VS issue that was fixed in a VS patch. Simply install from https://msdn.microsoft.com/en-US/library/mt634751(VS.140).aspx and it'll take care of it.
See also https://github.com/T4MVC/T4MVC/issues/67.

Related

Breeze expand navigation property not populating array

I am using Web API 2 with Breeze and Entity Framework 6. I have the following models.
public class ScoreCard: EntityBase
{
public string Title { get; set; }
public virtual List<ScoreCardSection> Sections { get; set; }
public String UserId { get; set; }
}
public class ScoreCardSection:EntityBase
{
public string Title { get; set; }
public int Index { get; set; }
public long ScoreCardId { get; set; }
public virtual List<ScoreCardQuestion> Questions { get; set; }
}
I want to fetch a score card with a list of sections. I execute a EntityQuery with an expand and it executes this URL:
/api/ScoreCards?$filter=Id%20eq%201L&$expand=Sections
I get back the following results:
[{"Title":"Catalyst Service Report","Sections":
[{"Title":"Worship","Index":0,"ScoreCardId":1,"Questions":null,"Id":1},
{"Title":"Message","Index":0,"ScoreCardId":1,"Questions":null,"Id":2},
{"Title":"Leader","Index":0,"ScoreCardId":1,"Questions":null,"Id":3},
{"Title":"Attendance","Index":0,"ScoreCardId":1,"Questions":null,"Id":4},
{"Title":"Security","Index":0,"ScoreCardId":1,"Questions":null,"Id":5}],
"UserId":"b9bd5256-dd18-4c1e-9907-dac9e7e4c01b","Id":1}]
So far, so good. Now we get to the problem. When I inspect the results during the success callback the sections property of the object is a empty array, even though the sections were sent back in the response.
What am I missing here? Thanks in advanced.
Ok, the solution to this problem seemed to be a few hours sleep.
For anyone who has this same issue the first part of the problem was that I didn't have a navigation property for ScoreCard on ScoreCardSection. Once I added that I got a "Self Referencing Loop" error. This was corrected by adding the BreezeControllerAttribute to my controller class.
Hope this is helpful to someone.

How to get BreezeJS metadata working

I'm getting this error with Breeze when I try to access the metadata url:
http://localhost:1886/api/posts/metadata
Self referencing loop detected for property 'PreviousAttribute' with type 'System.Xml.Linq.XAttribute'. Path 'root.firstAttribute.nextAttribute.nextAttribute.nextAttribute.nextAttribute.nextAttribute.nextAttribute'.
ExceptionType: "Newtonsoft.Json.JsonSerializationException"
My other URL is working and retrieving data from the database just fine
http://localhost:1886/api/Posts/posts
My post.cs has nothing that could be self referencing and looks like so
public class Post
{
public int PostID { get; set; }
public int UserID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
My controller:
[BreezeController]
public class PostsController : ApiController
{
readonly EFContextProvider<PostsDbContext> contextProvider = new EFContextProvider<PostsDbContext>();
[HttpGet]
public string Metadata()
{
return contextProvider.Metadata();
}
[HttpGet]
public IEnumerable<Post> Posts()
{
var posts = contextProvider.Context.Posts;
return posts;
}
}
I'm stuck on Windows XP using Visual Studio 2010 and EF 4 and .net 4. Are there version issues with Breeze and any of those?
The problem may be that Entity Framework generates proxy objects. Try turning these of
dbContext.Configuration.ProxyCreationEnabled = false;
Something in my project must have become corrupt, because I started over with a clean HotTowel template and the problem magically went away

Problems with Model binding and validation attributes with asp.net Web API

I am writing a Web API with ASP.NET Web API, and make use of the following View Model.
I seem to be having a problem with the data binding when there are two validation attributes on a particular property (i.e. [Required] and [StringLength(10)]).
When posting a JSON value from a client to a controller action of the form:
// POST api/list
public void Post([FromBody] TaskViewModel taskVM)
I observe the following:
If I remove one of the multiple attributes everything is bound OK;
If I leave in the multiple attributes, the client recieves a 500 internal server error and the body of the Post method is never reached.
Any ideas why this happens?
Cheers
public class TaskViewModel
{
//Default Constructor
public TaskViewModel() { }
public static TaskViewModel MakeTaskViewModel(Task task)
{
return new TaskViewModel(task);
}
//Constructor
private TaskViewModel(Task task)
{
this.TaskId = task.TaskID;
this.Description = task.Description;
this.StartDate = task.StartDate;
this.Status = task.Status;
this.ListID = task.ListID;
}
public Guid TaskId { get; set; }
[Required]
[StringLength(10)]
public string Description { get; set; }
[Required]
[DataType(DataType.DateTime)]
public System.DateTime StartDate { get; set; }
[Required]
public string Status { get; set; }
public System.Guid ListID { get; set; }
}
You need to inspect what is inside in the 500 internal server
make sure that you turn customerror off in your web.config
If you selfhost web.API you need to set GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
use your browser development console's network tab (in IE, Chrome you can get the console with F12) or if you are using FireFox then use FireBug or a thrid party tool like Fiddler.
Then you can see what went wrong on the server and go further to solve your problem.
In your case this is in the response:
"Message":"An error has occurred.","ExceptionMessage":"Property
'StartDate' on type 'MvcApplication3.Controllers.TaskViewModel' is
invalid. Value-typed properties marked as [Required] must also be
marked with [DataMember(IsRequired=true)] to be recognized as
required. Consider attributing the declaring type with [DataContract]
and the property with
[DataMember(IsRequired=true)].","ExceptionType":"System.InvalidOperationException"
So your problem is not that you have two attributes but that you've marked your properties with [Required] to solve this the exception tells you what to do.
You need to add [DataMember(IsRequired=true)] to your required properties where the property type is a value type (e.g int, datatime, etc.):
So change your TaskViewModel to:
[DataContract]
public class TaskViewModel
{
//Default Constructor
public TaskViewModel() { }
[DataMember]
public Guid TaskId { get; set; }
[Required]
[DataMember]
[StringLength(10)]
public string Description { get; set; }
[Required]
[DataMember(IsRequired = true)]
[DataType(DataType.DateTime)]
public System.DateTime StartDate { get; set; }
[Required]
[DataMember]
public string Status { get; set; }
[DataMember]
public System.Guid ListID { get; set; }
}
Some side notes:
You need to reference the System.Runtime.Serialization dll in order to use the DataMemberAttribute
You need to mark your class with [DataContract] and you need to mark all of its properties with [DataMember] not just the required ones.

Json.Net JsonConvert not deserializing properly?

I'm using Json.Net to handle the deserialzing of the response of API calls from the Pipl.com API in my application, and it works fine but for some strange reason it won't deserialize a specific property of the JSON string that I feed to the JsonConvert.DeserializeObject method.
My class is this:
public class Source
{
public string Dsname { get; set; }
public bool IsSponsored { get; set; }
public string Url { get; set; }
public string Domain { get; set; }
public uint ExternalID { get; set; }
public Source()
{
}
}
and everything except the Dsname gets deserialized properly. The Json to be converted is like this:
"source": {
"#is_sponsored": false,
"#ds_name": "Personal Web Space -MySpace",
"url": "http://www.foo.bar"
"domain": "myspace.com"
}
Any idea how to go about this problem? Thank you in advance.
I added a wrapper class and specified the property name as attributes, like this:
public class Source
{
[JsonProperty(PropertyName = "#ds_name")]
public string Dsname { get; set; }
[JsonProperty(PropertyName = "#is_sponsored")]
public bool IsSponsored { get; set; }
public string Url { get; set; }
public string Domain { get; set; }
public uint ExternalID { get; set; }
}
public class RootObject
{
public Source source { get; set; }
}
Then I was able to deserialize fine:
var json = "{\"source\": { \"#is_sponsored\": true, \"#ds_name\": \"Personal Web Space -MySpace\", \"url\": \"http://www.foo.bar\", \"domain\": \"myspace.com\"}}";
var des = JsonConvert.DeserializeObject<RootObject>(json);
Note that I:
- wrapped your sample in a curly braces to make it valid JSON
- added a missing comma
- changed the value of "#is_sponsored" to not be the default value to verify it is desearialized correctly.
Ok I realize, this is a pretty old thread. But I ran into a similar issue earlier and came across this thread.
In my case the class I was trying to se/deserialize had a List<ClassName> public property in it. Which serialized fine, but wont deserialize. I switched it to ClassName[] and the fixed the deserialization problem.
Hope it helps someone else who comes across this thread, or at least gives them something else to look for.

JSON Nullable Deserialization Error

Suppose you have a simple struct, like so:
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
And a sample class like so:
public class Map
{
public int ID { get; set; }
public Point? PointA { get; set; }
///...
}
Now, suppose you are passing Map via AJAX as JSON. Question, what value should be passed for the not null scenario?
It may matter that JavaScriptSerializer is being used in a C# 3.5 ASP.NET ASMX web service.
The issue is what I listed in my comment about the question. The automatic properties were the issue. I converted the property and the issue was resolved.

Resources