Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement...
Stats
ASP.NET MVC 5 Attribute Routing
Enable attribute route
Global.asax
  1. public class MvcApplication : System.Web.HttpApplication
  2. {
  3.     protected void Application_Start()
  4.     {
  5.         // Enable attribute route
  6.         RouteTable.Routes.MapMvcAttributeRoutes();
  7.  
  8.         AreaRegistration.RegisterAllAreas();
  9.         RouteConfig.RegisterRoutes(RouteTable.Routes);
  10.     }
  11. }
or
RouteConfig.cs
  1. public class RouteConfig
  2. {
  3.     public static void RegisterRoutes(RouteCollection routes)
  4.     {
  5.         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  6.             
  7.         // Enable attribute route
  8.         routes.MapMvcAttributeRoutes();
  9.  
  10.         AreaRegistration.RegisterAllAreas();
  11.  
  12.         routes.MapRoute(
  13.             name: "Default",
  14.             url: "{controller}/{action}/{id}",
  15.             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  16.         );
  17.     }
  18. }
Direct route
  1. public class BookController : Controller {
  2.  
  3.     // Map to 'http://prhythm.com.tw/Pet/10'
  4.     [Route("Pet/{id}")]
  5.     public ActionResult Pet(int? id) {
  6.         return View();
  7.     }
  8.  
  9. }
Optional paramter
  1. public class BookController : Controller {
  2.  
  3.     // Optional parameter id
  4.     // Map to 'http://prhythm.com.tw/Pet'
  5.     //     or 'http://prhythm.com.tw/Pet/5'
  6.     [Route("Pet/{id?}")]
  7.     public ActionResult Pet(int? id) {
  8.         return View();
  9.     }
  10.  
  11. }
Default parameter value
  1. public class BookController : Controller {
  2.     
  3.     // Default value of id
  4.     // Map to 'http://prhythm.com.tw/Pet'
  5.     //     or 'http://prhythm.com.tw/Pet/10'
  6.     [Route("Pet/{id=10}")]
  7.     public ActionResult Pet(int? id) {
  8.         return View();
  9.     }
  10.  
  11. }
Paramter constrains
  1. public class BookController : Controller {
  2.     
  3.     // Paramters constrains
  4.     [Route("Pet/{id:int}")]
  5.     public ActionResult Pet(int? id) {
  6.         return View();
  7.     }
  8.  
  9. }
ConstraintDescriptionExample
alpha Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z) {x:alpha}
bool Matches a Boolean value. {x:bool}
datetime Matches a DateTime value. {x:datetime}
decimal Matches a decimal value. {x:decimal}
double Matches a 64-bit floating-point value. {x:double}
float Matches a 32-bit floating-point value. {x:float}
guid Matches a GUID value. {x:guid}
int Matches a 32-bit integer value. {x:int}
length Matches a string with the specified length or within a specified range of lengths. {x:length(6)}
{x:length(1,20)}
long Matches a 64-bit integer value. {x:long}
max Matches an integer with a maximum value. {x:max(10)}
maxlength Matches a string with a maximum length. {x:maxlength(10)}
min Matches an integer with a minimum value. {x:min(10)}
minlength Matches a string with a minimum length. {x:minlength(10)}
range Matches an integer within a range of values. {x:range(10,50)}
regex Matches a regular expression. {x:regex(^\d{3}-\d{3}-\d{4}$)}
Route prefix
  1. [RoutePrefix("Booking")]
  2. public class BookController : Controller {
  3.  
  4.     // Map to 'http://prhythm.com.tw/Booking'
  5.     //     or 'http://prhythm.com.tw/Booking/Pet'
  6.     [Route]    
  7.     [Route("Pet")]
  8.     public ActionResult Pet() {
  9.         return View();
  10.     }
  11.  
  12.     // Map to 'http://prhythm.com.tw/Booking/Edit'
  13.     [Route("Edit")
  14.     public ActionResult Edit() {
  15.         return View();
  16.     }
  17. }
Override route prefix
  1. [RoutePrefix("Booking")]
  2. public class BookController : Controller {
  3.  
  4.     // Override route prefix
  5.     // Map to 'http://prhythm.com.tw/Pet'
  6.     [Route("~/Pet")]
  7.     public ActionResult Pet() {
  8.         return View();
  9.     }
  10. }
Default route
  1. [RoutePrefix("Booking")]
  2. [Route("action=pet")]
  3. public class BookController : Controller {
  4.  
  5.     // Map to 'http://prhythm.com.tw/Booking'
  6.     //     or 'http://prhythm.com.tw/Booking/Pet'
  7.     public ActionResult Pet() {
  8.         return View();
  9.     }
  10.  
  11.     // Map to 'http://prhythm.com.tw/Booking/Edit'
  12.     public ActionResult Edit() {
  13.         return View();
  14.     }
  15. }
Route area
  1. [RouteArea("Backend")]
  2. [RoutePrefix("News")]
  3. public class NewsController : Controller {
  4.  
  5.     // Map to 'http://prhythm.com.tw/backend/news'
  6.     public ActionResult Index() {
  7.         return View();
  8.     }
  9.  
  10. }
  1. [RouteArea("Backend", AreaPrefix = "Admin")]
  2. [RoutePrefix("News")]
  3. public class NewsController : Controller {
  4.  
  5.     // Map to 'http://prhythm.com.tw/admin/news'
  6.     public ActionResult Index() {
  7.         return View();
  8.     }
  9.  
  10. }

Reference http://sampathloku.blogspot.tw/2013/11/attribute-routing-with-aspnet-mvc-5.html

Reference http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

Asynchronous and deferred JavaScript execution explained
  • Normal execution <script>
    This is the default behavior of the <script> element. Parsing of the HTML code pauses while the script is executing. For slow servers and heavy scripts this means that displaying the webpage will be delayed.
  • Deferred execution <script defer>
    Simply put: delaying script execution until the HTML parser has finished. A positive effect of this attribute is that the DOM will be available for your script. However, since not every browser supports defer yet, don’t rely on it!
  • Asynchronous execution <script async>
    Don’t care when the script will be available? Asynchronous is the best of both worlds: HTML parsing may continue and the script will be executed as soon as it’s ready. I’d recommend this for scripts such as Google Analytics.

Reference : http://peter.sh/experiments/asynchronous-and-deferred-javascript-execution-explained/

Reference : http://www.html5rocks.com/en/tutorials/speed/script-loading/