locked
How to request an ID from URL RRS feed

  • Question

  • User-1557173817 posted

    Hello please how can i request an id from page url (current) and insert into a table.

    Thursday, September 10, 2015 5:02 AM

All replies

  • User-1980594115 posted

    It depends on how you are passing the url variables. Here are a couple of examples. Example 1:

    URL: http://localhost:4954/ReadUrlData/13/7/43:
    
    CODE: var urldata1 = UrlData[0].IsEmpty() ? 0 : UrlData[0].AsInt();
          var urldata2 = UrlData[1].IsEmpty() ? 0 : UrlData[1].AsInt();
          var urldata3 = UrlData[2].IsEmpty() ? 0 : UrlData[2].AsInt();
    
    RESULT: urldata1 is 13
    	urldata2 is 7
    	urldata3 is 43
    	
    

    Example 2:

    URL: http://localhost:4954/ReadUrlData?s=13&f=7&k=43
    
    CODE: var urldata1 = Request.QueryString["s"].IsEmpty() ? 0 : Request.QueryString["s"].AsInt();
          var urldata2 = Request.QueryString["f"].IsEmpty() ? 0 : Request.QueryString["f"].AsInt();
          var urldata3 = Request.QueryString["k"].IsEmpty() ? 0 : Request.QueryString["k"].AsInt();
    
    RESULT: urldata1 is 13
    	urldata2 is 7
    	urldata3 is 43
    

    Thursday, September 10, 2015 10:43 AM
  • User1313602441 posted
    I don't think it's advisable to insert into a database values gotten from a url. And there are good reasons not to.
    For all tasks that require updating, deleting, or inserting values into a database, it is recommended to always use "POST" request.
    But if you need to get a value (e.g. "ID") from a url, then you can use Dblaire's code above.
    Thursday, September 10, 2015 11:32 AM
  • User-1980594115 posted

     

    Normal practice is to send data for input into a database using the form "POST" and not the url, "GET". The HTTP standard is that url "GET" should only retrieve data, and not modify any data on the server, and "POST" requests are used for all data modifications.

    But don't be fooled, either method of transferring data can be hacked. Form "POST" is not so visible, but it does not offer true security by itself.  Both methods are not encrypted and can be evesdroped on. 

    Using SSL will add a layer of protection by encrypting the data being sent.  Any input going into a database should always be verified on the server level. Utilize the webmatrix tutorials on how to secure data being inserted into the database. 

    Friday, September 11, 2015 11:00 AM
  • User379720387 posted

    Here is something that has some of it explained:

    http://mikesdotnetting.com/article/192/transferring-data-between-asp-net-web-pages

    Sunday, September 13, 2015 6:07 PM
  • User-1557173817 posted

    Hello i tried retrieving  two values for : endance?id=1#myModal?code=4

    but was able to retrieve just on which is the no. 1, dont know the other number (4) was not retrieved. 

    here is my code:

     var studentid = Request.QueryString["code"].IsEmpty() ? 0: Request.QueryString["code"].AsInt();
        var classid = Request.QueryString["id"].IsEmpty() ? 0: Request.QueryString["id"].AsInt();
    Wednesday, October 28, 2015 5:20 AM
  • User753101303 posted

    Hi,

    The url is wrongly formed. It should be:

    endance?id=1&code=4#myModal

    With the current form you have just id=1 and myMode?code=4 as the https://en.wikipedia.org/wiki/Fragment_identifier which likely explains why you can't read the code value.

    Edit: not sure if you had this problem from the start but if yes, just ask directly about the problem you have rather than a general question about what you are trying to do. Else we'll just start with explaining what you likely have seen already while the real issue is something else (such as the url here) in your particular context....

    Wednesday, October 28, 2015 7:48 AM
  • User1935446700 posted

    Try this;

    @{
        var url = "";
    
        if(IsPost){         
            if(Validation.IsValid()){            
                url = Request.Url.AbsoluteUri;                 
                var db = Database.Open("ProjectDatabase");
                var insertCommand = "INSERT INTO PageUrls (Url) VALUES(@0)";
                db.Execute(insertCommand, url);            
            }
        }
    }

    Monday, November 2, 2015 3:48 PM