Pankaj
Pankaj

Reputation: 10095

Url to {replace} @ space with Hyphen in MVC-Razor

I am using ASP.NET MVC Razor C# with Attribute Routes

In my Category form when I click on Edit. it opens the Url like below:

http://localhost:38812/Category/Database%20Design

I am trying to change the Url like below

http://localhost:38812/Category/Database-Design

Here the problem is, when I go to database to search for the category based details..it does not search because there is no category with the name of Database-Design. It exist as Database Design with space.

Is there any way to not include the Id in Url and so we can also achieve the below Url? :

http://localhost:38812/Category/Database-Design

Upvotes: 1

Views: 1328

Answers (2)

Andres Castro
Andres Castro

Reputation: 1858

I think what you need to do is Decode the Url. You can do this by using

string decodedUrl = Uri.UnescapeDataString(url)

That will replace the %20 and return it back to how it was originally.

You can read more about this here https://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring(v=vs.110).aspx

Upvotes: 0

Rick Su
Rick Su

Reputation: 16440

you should create an extra column (Sanitized) in your Category table

something like below

 ID | Name            | Sanitized
---------------------------------------
  1 | Database Design | database-design
  2 | ABC-DEF_HIJ     | abc-def-hij
  3 | !Hello World!   | hello-world
  4 | happy~~feet     | happy-feet

By doing this way, Url and database query should make use of the Sanitized column

You wouldn't worry about what to replace with hyphen and it's all customisable.

Upvotes: 2

Related Questions