How can we hide the id from URL - c#

my current URL is:
http://example.com/country/state/location/ls5/a1392f1d-1aee-470c-a159-07e6da071620
but what I want in my URL is: http://example.com/country/state/location/ls5
so how can we do it?I am having trouble with it. please guide me for the solution

if you need the id to identify a record, then encrypt the id and use
something else that does not identify your record directly...
if you are not using the id, just remove it...
to answer your question fairly, everyone uses id in url in one way or
another, even stackoverflow ::
https://stackoverflow.com/questions/48515076/how-can-we-hide-the-id-from-url,
the id 48515076

Related

ASP.NET/C# - URL Rewriting or Shortening URL

Consider the following URL:
http://localhost:1234/Websites/Shop/ProductDetails.aspx/1234/Beauty-Cup
What is the best way to shorten the url and would still give me the same result?
http://localhost:1234/Products/1234/Beauty-Cup
1234 - Is a product code (this is where I retrieve the product's details)
Beauty-Cup - Is the products name (just for display purposes)
Basically, in the original path, I use Request.PathInfo to do my necessary checking and validation on the product code. This function is located in Page_Load of ProductDetails.aspx
Is there any better way to do this?
Thanks in advance!

Using a slug in URL & ID to query DB in MVC

I am trying to use slugs in an MVC web application but can seem to work out the best way to implement them.
I have found the the recommendation on how to create the URL friendly slug stackoverflow slug post
I still want to be able to query the Db with the ID but don't want this to be in the URL similarly to most stackoverflow URLs, for example
http://website/home/list/outdoor-products
How can a slug be displayed in the URL while still passing and using an ID to be used to query with?
It's doesn't really depends on a technology/framework which you are using, the main thing is you have to have destinctive urls to unambiguously select page content.
If you do have unique titles/slugs for pages, then you may use them as identity for content selection. Otherwise, you need to put some sort of id (it could be int or guid, whatever) into your urls. There isn't anything which will hide your int id behind the slug.
Talking about stakoverflow's urls, you'll find id just before the friendly title. Another option could be put actual id at the end of friendly title (friendly-title-1559063).

Fetch User ID / username from URL to display contents on a page [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to read parameter passed from asp.net page, using C#?
I want to display profile contents on a ASP.NET web page based on the user id which is displayed at the end of a the URL, as in a facebook page.
I want to be able to derive the id from the URL just so I can search the database to match the Id.
If someone could help me derive the Id which is the last bit of the URL, I could convert that to a string and do the rest.
Help on this is much appreciated.
Thank You
This will depend on how you're programming and the structure of your URL.
Example;
If the URL is something similar to http://mydomain.com/page.aspx?id=1 you can read the querystring by doing the following.
strID = Request.QueryString("ID");
In this instance the value of strID would be 1.
Are you using URL rewrite modules or simple Web Forms?
If you want to retrieve the name of the current page try:
Path.GetFileName(Request.Url.AbsolutePath);
My addition to the already solution:
string url = HttpContext.Current.Request.Url.AbsoluteUri;
This code will give you the URL but storing UserId and sensitive data in url is not good practice. My Advice will be to use Encoding techniques with query string.

Web forms and URL rewriting: Hide ID, show name?

I am working with URL rewriting, and have some issues. I want my url to be like this:
www.domain.com/product/myproduct
But I also want to be able to retrieve the ID of the product, without accessing the database. I thought about having a URL like:
www.domain.com/product/myproduct/1
or
www.domain.com/product/1-myproduct
But if I could hide the ID it would be better.
So, how do I do it the simplest way?
Currently my Global.asax has the following route:
routes.MapPageRoute("Produkt visning",
"legetoej/{Categoryname}/{SubCategoryname}/{ProductName}",
"~/SingleProduct.aspx");
And when I retrieve the name I do like this on SIngleProduct.aspx:
object productRoute = Page.RouteData.Values["ProductName"];
if (productRoute != null && !string.IsNullOrEmpty(productRoute.ToString()))
{
// do stuff
}
If I very simple could just get the ID instead of name, it would be awesome.
Thanks a lot awesome-stackoveflowers ;-)
To hide the ID of the Product... take the ID and apply Encryption to it... something like DES or Triple DES, then apply Server.URLEncode method to the ID and then you can easlily hide the id.
When you open the Page you can simple take the encrypted ID and do a Server.URLDecode and the decrypt the ID.
Anyways... why dont you pass the ID of the product as a get Parameter ? Something like...
www.domain.com/product/myproduct?PID=#$$#12 , where the PID is encrypted and URL Encoded and then you can easily process it.

ASP.NET MVC / C# - String to valid URL characters?

I don't know how to ask this, and I don't know what it is called either so I'll just describe what I want to achieve.
In the database, some articles' title originaly has spaces:
my title with spaces
But in the url, spaces are replaced by other characters such as plus sign (+) or underscore (_)
http://www.mydomain.com/mycontroller/myaction/my_title_with_spaces
or
http://www.mydomain.com/mycontroller/myaction/my+title+with+spaces
Now, how do you do that in C#? Or is there any helper in ASP.NET MVC that can do something like that?
Let say we achieved the said URL, is there any risk that two unique titles become the same in the URL? Please consider these titles:
Title's
Titles
after parsing, they became the same
Titles
Titles
This will be a problem when retrieving the article from the database since I'll get two results, one for "Title" and one for "Title's".
I would implement that functionality like this:
1. When creating a new article, generate the URL representation based on the title.
Use a function that converts the title for a suitable representation.
For example, the title "This is an example" might generate something like "This_is_an_example".
This is up to you. You can create a function that parses the title with rules you define, or use an existing one if it suits better your problem.
2. Ensure the URL representation is unique
If it's going to be an ID, it must be unique. So, when creating new articles you must query your database for the resulting URL representation. If you get a result from the database, it means the newly created article generated the same representation as one of the already created articles. Add something to it so it remains unique.
This could be something like "This_is_an_example_2". In this case, we added the "_2" to the end of the generated representation so it differs from the already existing one. Once more, with each change you have to ensure this representation remains unique.
3. Save the created ID in the database, along with the article data
In the database be sure to save the "This_is_an_example" ID and relate it to the article. Maybe even as the table primary key?
4. Query the database for the correct article
Now, about showing a site visitor the correct article:
When a visitor asks for the following resource, for example:
http://www.mydomain.com/mycontroller/myaction/this_is_an_example_2
Extract the URL part that identifies the article, in this case "this_is_an_example_2".
When you have that, you have the identifier of the article in the database. So, you can query the database for the article with the "this_is_an_example_2" ID and show the article's content to the user.
This might involve some URL rewriting. Unfortunately I'm unable to help you with that in asp.NET. Some search on the subject will surely help you.

Categories

Resources