a developer's notes – a semi-technical web development BLOG

May 15, 2015

Have Visual Studio build your MVC Views to check for errors.

Filed under: ASP.NET MVC — Duy Nguyen @ 5:41 pm
Tags: , , , ,

Step 1

Unload Project – Right click on project in solution explorer, select Unload Project.
Step 2

Edit Project – Right click and select ‘Edit Project and project XML will load in the editor window.
Step 3

Find false in Project XML file and change this to true.
Step 4

Save the Project XML file.
Step 5

Reload Project – Right click on the project and select ‘Reload Project’.

Now when Project compiles, views will also be compiled.

Advertisements

January 28, 2015

TFS 2010 Build Definition and Transform Files

Filed under: ASP.NET MVC,C# — Duy Nguyen @ 2:52 pm
Tags: , , ,

Here is a great blog that helped me with my build definition work with my transform files.

http://www.andygeldman.com/index.php/2011/10/web-and-app-config-transformations-with-tfs-build/

How do you specify multiple configurations like debug and release to get built by MS Build? See this answer.

http://stackoverflow.com/questions/14068750/changing-config-based-on-build-definition-tfs-2012

Some useful urls:
http://stackoverflow.com/questions/6346556/how-do-i-target-a-specific-net-project-within-a-solution-using-msbuild-from-vs2
https://msdn.microsoft.com/en-us/library/dd647547%28v=vs.110%29.aspx#projects

January 22, 2015

Cookie Tempdata Alerts with ASP.NET MVC

Filed under: ASP.NET MVC — Duy Nguyen @ 10:19 pm
Tags: , , ,

https://github.com/duyn9uyen/CookieTempDataProvideAlerts

A sample MVC Project using the CookieTempDataProvider and Bootstrap alerts. This project will illustrate how to add notifications on the serverside and client side via javascript.

Add the CookieTempDataProvider code We used the CookieTempDataProvider from:
https://github.com/KunleSmart/MvcExtensions/blob/master/src/CookieTempDataProvider.cs

Add a basecontroller that uses the CookieTempDataProvider
Added Bootstrap to project using NuGet for styling -(Added the reference to bootstrap.js and bootstrap.css in the _Layout.cshtml file.
Added notification icons (used http://www.famfamfam.com/lab/icons/silk/)
In _Layout.cshtml, moved the scripts into the tag (so that jquery will work) @Scripts.Render(“~/bundles/jquery”) @RenderSection(“scripts”, required: false)

December 7, 2014

Adding Cookies to Response, adds to the Request – Removing cookies from the Request

Filed under: ASP.NET MVC,HTML — Duy Nguyen @ 10:42 am
Tags: , , , ,

I don’t know why but when you add cookies to the RESPONSE, it seems to add the same cookie to the REQUEST. I was able to pin point this because I notice in my request, I had a bunch of cookies that I had previously expired from the response. Typically, when you expire cookies, you set the expire date time and add it back into the response.

Also, make sure you remove a cookie from the request AFTER you expire the cookie from the response. Otherwise, the cookie will still be available in the Request later in the process cycle.

September 9, 2014

MVC PartialView() VS View()

Filed under: ASP.NET MVC — Duy Nguyen @ 10:36 am
Tags: , , , , ,

Use PartialViews when you want to return a view that does that need the layout.

return PartialView()

Use Views when you need to return a view that supports the layout file.

return View()

A good blog post about it can be found here.
http://completedevelopment.blogspot.com/2014/01/is-there-really-no-difference-between.html

August 21, 2014

“The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.”

I keep on getting this error in Visual Studio when trying to connect to a new Secured SSL site.

“The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.”

After I installed the certificate, I was able to continue working.

Note: Also, check if Fiddler is running. It may cause issues on SSL connections.

August 5, 2014

How to Redirect user or execute Controller Action in a Custom MVC Filter

Filed under: ASP.NET MVC,C# — Duy Nguyen @ 12:41 pm
Tags: , , , , ,

MVC Filter

    public class MyFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // The controller will be the controller you add the filter to. Or some base controller
            var controller = filterContext.Controller as YourController;

            // Execute controller action from filter. 
            controller.SomeControllerMethod();

            // Redirect to controller action
            filterContext.Result = new RedirectToRouteResult( new System.Web.Routing.RouteValueDictionary
            {
                {"controller", "mycontroller"},    
                {"action", "myaction"},
                {"querystring", "?hello=true"}
            });

            // Redirect to external url
            filterContext.Result = new RedirectResult(url);
        }
    }

Preventing Browser Cache with a MVC Controller Filter

Filed under: ASP.NET MVC — Duy Nguyen @ 12:04 pm
Tags:
public class NoCacheFilter : ActionFilterAttribute, IActionFilter
    {
        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();
            filterContext.HttpContext.Response.Cache.SetAllowResponseInBrowserHistory(false);

            this.OnActionExecuting(filterContext);
        }
    }

You can declarate your controller with this filter and if the user clicks on the back button, the MVC routing will attempt to (re)retrieve data from your app.

April 30, 2014

Invoke a MVC Html Helper Method in a Unit Test

Filed under: ASP.NET MVC — Duy Nguyen @ 9:14 am
Tags: , , ,
HtmlHelper helper = null;
helper.YourHelperMethod();

April 2, 2014

Using HTML5 Data Annotations for Zipcode and Telephone Numbers

Filed under: ASP.NET MVC,Javascript / JQuery — Duy Nguyen @ 11:22 am
Tags: , , , , , ,

Setup your view model properties with HTML 5 attributes:

[Required]
[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }

[Required]
[DataType(DataType.PostalCode)]
public string ZipCode { get; set; }

Then in your script file, you can have controls use jquery.MaskedInput and also add the bootstrap css class “form-control”

    setupMaskInputs: function () {
        // Any view model property that has a [DataType(DataType.PhoneNumber)] attribute, renders type=tel
        if ($("input[type='tel']").length > 0) {
            $("input[type='tel']").mask("(999) 999-9999? ext.9999", { placeholder: " " });
        }

        // Any view model property that has a [DataType(DataType.PostalCode)] attribute, renders type=zip
        if ($("input[type='zip']").length > 0) {
            $("input[type='zip']").mask("99999?-9999", { placeholder: " " });
        }

        // @Html.EditorFor is used to render any inputs that need the html 5 type attributes.
        // But @Html.EditorFor, won't allow you to add a css class, so we do it here.
        $('input[type=text],input[type=tel],input[type=datetime],input[type=date],input[type=number],input[type=password],textarea,input[type=email],input[type=url],input[type=zip]').each(function (idx, item) {
            if (!$(item).hasClass('form-control'))
                $(item).addClass('form-control');
        });
    }
Next Page »

Blog at WordPress.com.