Tuesday, 30 July 2013

Restrict to numeric id only in mvc routing

Suppose we have defined the following route in our application and you want to restrict the incoming request url with numeric id only. Now let's see how to do it with the help of regular expression.
routes.MapRoute(
 "Default", // Route name
 "{controller}/{action}/{id}", // Route Pattern
 new { controller = "Home", action = "Index", id = 
 UrlParameter.Optional } //Default values for parameters
);

Restrict to numeric id only

routes.MapRoute(
 "Default", // Route name
 "{controller}/{action}/{id}", // Route Pattern
 new { controller = "Home", action = "Index", id = 
 UrlParameter.Optional },//Default values for parameters
 new { id = @"\d+" } //Restriction for id
);

Now for this route, routing engine will consider only those URLs which have only numeric id like as http://abc.com/Admin/Product/1 else it will considers that url is not matched with this route.

Monday, 3 June 2013

Get the url to the virtual root for the current request

Example(1)
Uri requestUri = Context.Request.Url;
string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);


Example(2)
This can be done much easier with the following code:
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

Wednesday, 1 May 2013

http - How to check if a file exists on a server using VB.net

 Dim _Result As String
 Dim url
 url = dt.Rows(i)("ImageUrl").ToString()

 Dim myRequest As System.Net.HttpWebRequest = System.Net.WebRequest.Create(url)
 Dim myResponse As HttpWebResponse

   Try
    myResponse = myRequest.GetResponse()
 '_Result = myResponse.StatusCode.ToString();
    _Result = "1"
   Catch ex As System.Exception
    _Result = "0" 
   Finally
    myResponse.Close()
    myRequest = Nothing
    myResponse = Nothing
    GC.Collect()
   End Try
   Response.write(_Result)

Friday, 12 April 2013

How to use Google Maps with marker in ASP.NET, MVC

Copy-paste this code

<script type="text/javascript"  src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/src/markermanager.js"></script>


<h2>MAPS</h2>
<div id="map_canvas" style="height: 400px; width:100%;"></div>

<script language="javascript" type="text/javascript">
    var map;
    var mgr;

    function initialize() {
        var myOptions = {
            zoom: 14,
            center: new google.maps.LatLng(26.32849908419746, 73.11118125915527),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        mgr = new MarkerManager(map);
        var infoWindow = new google.maps.InfoWindow({ content: "SmartSign" });
        google.maps.event.addListener(mgr, 'loaded', function () {

        
                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(26.32849908419746, 73.11118125915527),
                    html: "Om Prakash Bishnoi, House No : 83, Bheru Nagar, Banaar Road, Jodhpur, Rajasthan"
                });

                google.maps.event.addListener(marker, "click", function () {
                    infoWindow.setContent(this.html);
                    infoWindow.open(map, this);
                });
                mgr.addMarker(marker, 0);
            mgr.refresh();
        });
    }
    $(document).ready(function () {
        initialize();
    });
</script>

Friday, 29 March 2013

javascript maintain page scroll position

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
 <title>Test Page</title>
 <script type="text/javascript">
 function callTest(value)
 {
   alert(value);
 }
 </script>
</head>

<body>
 <form id="form1" runat="server">
  <table>
   <tr>
    <td style="height: 300px;">
     <a href="javascript:void(0);" id="A1" onclick="javascript:callTest('Add');" >Add</a>
     <p> Description 1............</p>
    </td>
   </tr>
   <tr>
    <td style="height: 300px;">
     <a href="javascript:void(0);" id="A2" onclick="javascript:callTest('Edit');" >Edit</a>
     <p> Description 2............</p>
    </td>
   </tr>
   <tr>
    <td style="height: 300px;">
     <a href="javascript:void(0);" id="A3" onclick="javascript:callTest('Delete');" >Delete</a>
     <p> Description 3............</p>
    </td>
   </tr>
   <tr>
    <td style="height: 300px;">
     <a href="javascript:void(0);" id="A4" onclick="javascript:callTest('View');" >View</a>
     <p> Description 4............</p>
    </td>
   </tr>
  </table>
 </form>
</body>
</html>

Get Page Name In Javascript ??

  <script type="text/javascript">

        var sPath = window.location.pathname;
       var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
       alert(sPage);

</script>

Thursday, 28 March 2013

Get Cookie Value From Javascript ??

function getCookie(cookiename)
  {
  // Get name followed by anything except a semicolon
  var cookiestring=RegExp(""+cookiename+"[^;]+").exec(document.cookie);
  // Return everything after the equal sign
  return unescape(!!cookiestring ? cookiestring.toString().replace(/^[^=]+/,"").replace("=","") : "");
  }

var myCookie = getCook('cookieName');