Friday, October 28, 2016

Another my own memo about MVC Scaffolding

First of all, I was not familiar with the word “scaffold”. An online dictionary defines it as “a temporary structure for holding workers and materials during the erection, repair, or decoration of a building.” My understanding of it at this moment is a tool to generate other constructs of a MVC project, such as views and controller, from a model class.

So let us start some tries and errors.

First I create my model class.

image

Then, give a try to the built-in menu to create the controller.

image

image

image

NOTE: I choose to create the new data context class.

Give it a try to see if the site works for the model. It fucntions nicely to me. Then what is this New Scaffolded Item?

image

Let us give it a try.

image

This appears does exactly the same as the Add > Controller...

The next and finally, there is a NuGet package called MvcScaffolding.

It can be used in the Package Manager Console as follows.

PM> Scaffold Controller Team -DbContext TeamContext

NOTE: If omitted, and none exists, a DbContext class is created with the project name.

The result effectively is the same. It creates the controller and views. Though, the code generated are different (taste of the author perhaps)

TIP: This tool does not like my model class as it is shown above, and produces an error: Get-PrimaryKey : Cannot find primary key property for type 'WebApplication9.Models.Event'. Multiple properties appear to be primary keys: Id, EventId

It appears that it does not recognize the [Key] annotation, and takes as the primary key either the column ID or {classname}ID. Read more: https://social.msdn.microsoft.com/Forums/en-US/2cef7375-e832-4f98-9084-4bf0558e57f1/getprimarykey-cannot-find-primary-key-property-for-type?forum=adodotnetentityframework

In the actual project that I work in, for this Event model, I need ths EventID, which uniquely identifies an Event, and is defined and assigned outside of my peoject. Apart from it, there need to be another ID which would be populated automatically when it is created into the corresponding database table. I named the first ID as EventID because It has been calld as such in the existing system, but then stepped on the land mine.

There seems no workaround. I renamed it as _EventID.

Anyway, one advantage of this package over the built-in tool is that it could creates the Repository class.

What is a Repository class?

http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/ reads: TeamController reads and writes the data in SoccerSiteContext directly. That’s fine in many simple scenarios, but if you want to decouple your controller logic from persistence logic a little (e.g., so that you can write clean unit tests for the controller), you may prefer to reach your data through an interface.

And this seems is the standard architecture for the next version of the Framework; ASP.NET Core MVC. I understand that the framework that is developped as ASP.NET 5 is now renamed as ASP.NET Core 1.0.

The MvcScaffolding creates the repository class, and then the contoller as follow.

PM> Scaffold Controller Team -DbContext TeamContext -Repository

Tuesday, March 31, 2015

Hit highlighted summary

Haven’t you ever wondered why for some results, the summary does not contain any highlighted word?

image

Although the found document file does contain the searched word.

After having spent sometime, I think I found the explanation.

Basically, it is explained at http://goo.gl/RiSFBx.

In short, SharePoint keeps only 10,000 characters for each indexed file for the summary, and highlights the searched word only if it appears in the saved text.

Wednesday, September 17, 2014

14 and 15 hives, and upgrade solution/features

 

This must not be new and I seem have done this at least once back February, because for the master page in question, I have the same file dated back in February both in the 14 and 15 hives.

But I have forgot completely what I did back then, and spent the entire morning just to update the same master page file. So this time, I write here what I learnt (again) today, so it would be clearer what to do for the next time.

The conclusion first. I need to run this cmdlet:
Install-SPSolution -Identity (the solution file name) –WebApplication (web application name) -FullTrustBinDeployment -CompatibilityLevel All –Force

We are operating a site that we have upgraded from 2010 (we started with 2007 actually). Therefore (I do not know now if it was THE way to go but), FEATUREs that we have been using from the time of 2010 including this master page that I wanted to update today, reside in the two hives.

For FEATUREs that I need to update since the upgrade to SP2013, I have specified SharePointProductVersion="15.0" in their solution manifest, and run simply stsadm –o upgradesolution (I prefer STSADM to PowerShell). They are correctly updated. We have got what we want in the effect.

Today, however, I found one shortcoming (I must have in February too), which is that the ghosted image of the master page that you see in SharePoint Designer stays as it was before i.e. not updated.

First I thought it is probably due to the cache, but at the end concluded that SPD looks into the 14 hives rather than 15. When I update the file in the 14 hive as well as 15, with the above cmdlet, you see properly the updated image in SPD.

I think this is absurd. I do not see any logic. But it appears to be the way it works...

Monday, August 5, 2013

Network share on server 2008 R2 cannot be opened from XP

 

http://panerarichang.blogspot.ch/2012/01/tip-cname-windows-server-2008-r2-x.html

I found this post very useful, after many many not useful ones which, first of all do not explain the reason and just blindly propose solutions.

Some might find my own experience useful. I found the registry key already set, but the problem was always there. XP clients cannot open the share. I removed it and put it back. Then the issue is removed.

The above post explains that that is the solution, with the reason very clearly explained. Otherwise, I would not have tried the remove/add-again solution.

Tuesday, July 2, 2013

WSS_KeepSessionAuthenticated

As becoming the de-fact, when asking users to authenticate, we change i.e. redirect to SSL. But then, if he/she opens another browser window and comes to us on http, to view two pages from our site simultaneously, on the second windows, he/she goes back anonymous. He/She has authenticated on the SSL i.e. https session, but not on http.

We first implemented this mechanism on SharePoint 2007. We may have wanted to redirect the second session to SSL so that the user finds himself logged-in on both windows. But due to some technical constraint not really of SharePoint, it was not that straightforward.

Anyway, this is how we started.

Then, when upgraded to 2010, we saw that the user is asked for username/password on the second session, even though the page that he is trying to open is anonymously accessible. After some research, we found out that this seems due to the WSS_KeepSessionAuthenticated cookie, that is sent. SharePoint forces the user to authenticate when it finds the cookie in the request.

To overcome, we created and hooked up a HttpModule to clear it at the beginning of the request made on http. It is an acceptable workaround for us, because we do not ask users to send their passwords as clear text.

Today, I found out that it is no longer needed with SharePoint 2013. The problem was that with SP2010, the cookie was set as non-secure. It is set when the user authenticates, and sent back to the server whether the session is secure or not. While with SP2013, it is set as secure when authenticated on https, and is sent back only when the communication is secured i.e. on SSL.

Wednesday, April 24, 2013

SharePoint2013 migration resetting master page references

If I am not wrong, it does, isn’t it?

Actually, it is not new. It did so when migrating to 2010 also. It changed master page reference of all sites to v4.master. I wrote a code to reset them back to our custom master. Needless to say that, before doing that, the custom master needed to be adapted. I needed the code to do this, because, even with sites where you see the master page reference is inherited from its parent, the effective master page became to v4.master.

I did not think it a big flaw at that time, but now think it is. What if for a legitimate reason, an organization employs several masters for their public facing web site, say for instance, employing different brandings for different products, champagnes etc.? Once upgraded, you do not know any more which site was on which master.

If SharePoint does not allow such an arrangement i.e. employing different master page for different sites, then OK. We would not complain. But it does allow, even encourages you, and says NO you cannot, all the sudden, when a new version comes out.

I did some googling, to find out that not so many seem concerned. Is this the sign that it has never gained enough popularity in the public facing web sites market? Where obviously branding is one of the most important aspects.

Monday, November 5, 2012

'premature end of script headers' in Apache

This is just a good-to-remember for myself.

When Apache reports this error with your CGI program, a script most of the case, you need to understand, or to know what the error message means, or what Apache complains about. Which I found many posts that I came across talking about the error do not really explain.

As you know, output of any CGI program must take the following form.

HEADERs
e.g. Content-type: text/html

A blank line

The payload. Normally a <HTML> element.

Apache complains with the message that the output fails to take the form.

Luckily, for the Perl script that I had the problem with, it was rather easy to locate and eventually remove the issue.

This was the good reference. http://www.linuxjournal.com/article/1367 It explains how to run a Perl CGI script from the command-line.

The point is that when you run it, it waits for input, for any possible POST payload. You indicate the end of the input with a control+D. Then it runs and you can see the output on your terminal. Once you get it, locating the issue should be (normally) straightforward.

In my case, the issue turned out that an external executable called from the script ends abnormally, and therefore corrupts the output.