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

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.

Related

How can we hide the id from URL

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

how can i handle pagination in asp.net C# MVC4 in advance search module?

Actually in my application there are two type of searching facility one is Quack search and another is Advance search
and i use routing the url means when i use Quack search with computer then my url is
1) http://www.example.com:1270/Computer-Company/Computer/1
we have more TABS or BUTTON on screen like Projects,Services,Leads....etc by default selected company TABS/BUTTON you can click on any tabs/button
suppose you search with computer then by default company is searching, then you can click on Project, you can see the result computers project and url is like below
Computer-Projects/Computer/
and pagination is working also fine Because we have only one Searching text like computers
But my problem is in Advance search
THERE ARE THE TWO TYPE OF PROBLEM
=> when i search with multiple criteria like Company Name,Contact Person,Tags,....etc
User may be search with all criteria or single criteria so ADVANCE SEARCH URL IS LIKE
3)http://www.example.com:1270/AdvanceSearch/Company/1
so please let me know how can i pass the all data from view to controller at pagination time.
so please let me know how can i pass the data from view to controller with the use of model
Temporary i am passing hidden data,But in controller side parameters write like
public ActionReslut Company(string companyName,string contactPerson,string country.....)
{
that is not proper way
}
You should use a ViewModel that contains all the fields that you need in the search:
public class SearchParameters{
string companyName {get;set;}
string contactOerson {get;set;}
string country {get;set;}
....
}
Your action:
public ActionReslut Company(SearchParameters searchParameters)
and then using these fields to filter your data.

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).

How to use a session variable or cookies

I have a main page with a large list of companies, I have a search button and if I want to find a company by state or city I can filter my search.
I decide to filter my search by cities, lets say Chicago, and I get 10 companies as a result of my search.
I click any company in that list and I go to the company details but I realize that is not the one I am looking for, so I want to go back to my 10 companies result list but instead I go back to the main company search interface and I have to re-start my search again.
How to use a session variable or cookies for this case?
Somebody can help me with this?
Another valid option not involving Session or cookies would be using the url for passing search parameters. If your site is public this may also help you with SEO.
You could use a friendly url + rewriting rules or just querystring if you are not using any rewriting.
On the company details page you'd store the referrer url and if the user wants to come back, you do it to that page.
I agree with Claudio, but if you do want to use session I would do something like the following
In page load do something like this
if(!Page.IsPostBack)
{
string filter = Session["SearchQuery"].ToString();
if(filter != null)
{
FilterResults(filter)
}
}
And then in your filter event do something like
Session["Filter"] = txtCityName.txt;
FilterResults(txtCityName.txt);

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