Showing posts with the label MVC 4.0

My Orchard CMS Experience


Updating MVC4 to MVC5

In my last article on MVC, I updated Razor 2.0 to 3.0 and showed the difficulties of updating the Razor nuget package in a MVC application. This article picks up from there and since Razor 3.0 is dependent on MVC5 I can assume you have updated Razor already.

Typical errors you will see..
Method not found: 'System.Web.WebPages.IDisplayMode System.Web.Mvc.ControllerContext.get_DisplayMode()'.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.MissingMethodException: Method not found: 'System.Web.WebPages.IDisplayMode System.Web.Mvc.ControllerContext.get_DisplayMode()'.
Updating the System.Web.WebPages to version 3.0 fixed this issue. However you may see more packages issues like it.

What you can do is try reinstall each package that appears to be broken like so..
Update-Package –reinstall <packageName…

Asp.Net MVC: Error Handling Ajax Partial Views

During the second year of my job as a Application Developer I was ask to design some type of error handler to properly display partial views that were pulled in via ajax. What was currently happening was that partial errors were being displayed as full page error and causing other issues when Jquery libraries being loaded in twice.
Questions Could you create a partial view and full view error page for display appropriately for both? So in the case where it was a partial view error the error page would simply display inside the partial.Is there a way to determine by ActionResult type the error handler needed?My Solution ///This is an attribute for PartialViews. It handles any errors and override the basecontroller's OnExpection method. public class PartialErrorAttribute : HandleErrorAttribute { ///Similar setup to the base controller OnExpection method, but has a special partial view error page called _ErrorPartial".<summary> ///This OnException wi…

Using Entity Framework and a Storage Procedure with Parameters

So I was stuck sometime ago on a problem I had where I was using my sql store procedure with the Entity Framework. Eventually found what I needed and the purpose of this article is just to write out a small example. I've come up with multiple ways to achieve the same thing now but this is the most basic form of it.

public IList<KeyValuePair> GetIds(string id) { using (var db = DBFactory.GetDB()) { return db.Database.SqlQuery<KeyValuePair>( "EXEC db.MyDatabase @id", new SqlParameter("id", id)); } }
Resources How to pass parameters to DbSet.SqlQuery Method in Entity Framework How to use DbContext.Database.SqlQuery(sql, params) with stored procedure? EF Code First CTP5 The data reader has more than one field. Multiple fields are not valid for EDM primitive types
KeyValuePair(TKey, TValue) Structure (System.Collections.Generic)

Unauthorized Ajax Handler for MVC

     I need respond to users that they are unauthorized, and I need it to not return a redirect to them from a ajax call which MVC does by default. A even better solution would be that I also do not force users to refresh page they are currently on and instead give them the ability to re-login via ajax on that current screen. This would allow users to save any  information they are currently working on. I should warn the user that it they are no longer logged in and if possible prompt them and within that same prompt allow them a way to login back in all without refreshing or a redirect. For now I'm going to just focus on the first problem which is suppressing Asp.Net's default behavior with unauthorized calls and do this just for Ajax calls.

Solution - Thanks to Joe Harrison for missing pieces! 
//C# code override an Authorization Attribute create a customized one.
protectedvirtualvoid HandleUnauthorizedRequest(AuthorizationContext filterContext)
{ if (fi…

My Introduction to TeamCity

The purpose of this article is simply to record and document my experiences using Teamcity. I know later down the road I will have a lot more questions about TeamCity but for now just I wanted to write down what I already know and to describe some of the ways that I use it.

    I first was introduced to TeamCity in June 2013, where it was setup to automate builds and stared builds based the changes in SVN. Using this setup on daily basis, I could see my commits and others as well. Depending on if it was developing or production code teamcity would build code accordingly and put those changes in a Pending state (or Build Queues). Much like TFS, TeamCity allows me to see the history of changes and who has committed what. One thing I liked the most was that TeamCity would not publish builds that failed. Which was a critical step when developing, especially when pushing to prod  since it gave me a quick double check before going live with my code.

Future Questions
     Down the road I…

MVC Excel Email Attachment with NPOI

The First Question I had after successfully creating a Excel sheet was how do I create an attachment from a stream? A second to be more specific was how do I add an attachment to an email using System.Net.Mail? Ultimately I wanted to have the Excel Exports in C# using NPOI but I started small and thankfully others had already asked and answered these questions. After which was just a matter of knowing What the correct content-type for excel files was?
First Attempt
MailMessage mail = new MailMessage(); HSSFWorkbook workbook = CreateExcel(OrderId); MemoryStream ms = new MemoryStream(workbook.GetBytes()); mail.Attachments.Add(new Attachment(ms, "example.xls", "application/")); _emailService.SendEmail("", "Email test", "Testing 123"); Changed it to.. MemoryStream ms = new MemoryStream(); workbook.Write(ms); This was based on the QA "NPOI - Sending workbook as ema…

MVC Data Exported to Excel with NPOI

This is an article for just writing an Excel file using NPOI and I want the quickest and easiest way to export a excel file from MVC. To keep everything in work fluid motion I will keep the user on the same page and do a forced download within that page. This is a setup that I have seen in multiple times and I think works great in today's modern website designs.

Creates excel files
Leniel Macaferi's blog: Creating Excel spreadsheets .XLS and .XLSX in C#
Create Excel Spreadsheets Using NPOI
var workbook = new HSSFWorkbook(); var ExampleSheet = workbook.CreateSheet("Example Sheet"); var rowIndex = 0; var row = ExampleSheet.CreateRow(rowIndex); row.CreateCell(0).SetCellValue("Example in first cell(0,0)"); rowIndex++; Problems I faced XLS is not the same as CVS which is obvious but truthfully I did not know the difference between the two file types (other than their different extensions). So I asked myself what is the Difference between CSV and XLS files? W… MVC4 - Bundling and Minification

While this article may be very simple and straightforward, there are a few concepts here I think are important to note. In my next article will be looking more in depth on the idea of creating a customized Web.config, but for now with this article, I just want to look at activating the bundling and minification feature by using the xml tags inside a MVC's Web.config. This feature is only available in ASP.NET 4.5 and above.
The First Problem
With the following inside your Web.config... (transform is with a Captial T.)
And with either of the following inside your build config or customized Web.config (I.E. Web.Release.config)
This also can be achieved by doing the following...
     In both snippets of code you do not need to use the "xdt:Locator" attribute (used only in cases where there are multiples elements of the same name .  What you may not see is that in the Web.config we have the "location" tag wrapped around the "system" tag. This is important …

Updating Razor 2.0 to 3.0 with MVC

Currently updating an application from MVC 4.0 to MVC 5.0 but before I update MVC I wanted to update the Razor package. Since the MVC package is dependent on the Razor. So this article is about Razor 2 to Razor 3 and the problems I faced.

Razor in my opinion
Razor 3.0 (details in the link) is an awesome package in my opinion and I shudder to think anyone who writes MVC applications using ASPX. For me the Razor syntax is an effective way to write visually clean code and because of this provide very low maintenance.

Issues with Razor Updating from 2.0 to 3.0
As I updated I encounter numerous problem with the Razor package from 2.0 to 3.o and not only did it break my application but destroyed the Intellisense for Razor (mostly for ViewBags).
So for my first problem...
Inheritance security rules violated by type: 'System.Web.WebPages.Razor.WebPageRazorHost'. Derived types must either match the security accessibility of the base type or be less accessible.
Which c…

An Introduction to Solrnet

Introduction      This article about the Solrnet project as you might have guessed from the title.. but you might ask what is Solrnet and why you would want to add it to your MVC application? The answer to that question is indexed searching! You probably should have an understanding of what the Apache Solr Project is and if you don't know about this project you probably would be just as lost as I was just a month ago. As a .net developer I rarely step into the Java world, but at times it's helpful to see how the other half live.  Don't get me wrong I love Java as much as I love C# (they are more alike than different) but for a lot of my applications; I don't really have a reason to. However the Apache Solr Project changed my mind, and if it hasn't already it should change yours. I'm not talking about jumping ship and using Java but a little doesn't hurt and with Solrnet you really don't need to use any java (just run it the side). 
Some background    T…

Adding Google Sign-in And Others

Introduction     The purpose of this post is to show how to add any Oauth login to your site, but mostly Google Plus because of my current applications (Penguins Rising!). In my quest to achieve this I found it to be really simple with Google Plus and the only real trouble I had was with the Client Id. I setup my Google API service account years ago but never really tried anything until now. What I did was just recreate a client id and everything worked!

My Website Oauth Choices       This next part is a little of my opinion and about what I think is the best strategy for Oauth integration. For me when it comes to social medias and integrating user logins I like to go the route of making sure I have all three of the major. The major hitter in my book are Twitter, Google Plus, and of course Facebook. However I know a lot of sites differ on the importance these three . Still personally I like seeing Google Plus being available and I think any site that has the other two but not Google …

Northern Safety Career and Personal Achievements

Opening Statement     It comes as a great sadness to say that I am no longer continuing my services at Northern Safety as a Application Developer for the IT department. I would like to state that I had a wonderful time working with everyone within the company and hold no personal grudge against anyone or any decisions that were made. This was a neutral agreement and final decision made by management and myself. I would  just like to state some of  my achievements while I worked Northern Safety, and I will try to be less technical for readers with less of a computer relate background.

Co-op Experience    As a co-op, at Northern Safety I started out working with Orchard CMS. Forthose who do not know what Orchard is, Orchard is a free and open source project that focuses on content management and built off of the same framework used the Northern Safety site ( Content such a paragraph or the body of a web page are structured and organized in a way for the website to work with ot…

What is the Rust Spot?

The Idea     A lot of times When I try to explain an Idea, I find it increasingly difficult to explain the more complex it is. So I will begin to explain it here in a nutshell. I want to create 3D models of my car and all the parts that it is made of. Simple right? Now I won't go into details of why just yet but I will say you have no idea how many parts making up your car. So the complex idea is this, I want to create a website that not only creates a 3D model of all the parts on a car but compares the parts for compatibility with other makes and models. To go further with at by give a complete break down of the car and instruction on installation or remove of parts. Still with me?
Good here is a example of what a normal diagram of a car part...

The Problem     Now from the picture you have a general idea of what your parts look like, but what about the placement of where it is in your car? Maybe your looking at the wrong model? Cars have a number of key differences between mode…