Need some help regarding .net mvc URL routing/rewriting - asp.net-mvc

URL is something like
/home/rawstring13245/rawstring534533453
I want the rule that saves only 13245 to parameter and 534533454 to another but ignore raw strings before them.
how to achieve it in route.config file?
i want this because strings are not parameters , I need only parameters out of string
like:
url:"{controller}/rawstring{action}/rawstring{id}",
what to enter in place of raw string? I don't need those strings. and yeah each raw string is of same length= 10

You can get complete variables as strings and get your desired part by using the sub-string method. Its the quickest solution, i thought it solve your problem.

Related

URL Encode string for Href ASP.NET MVC / Razor

I'm trying to build an Href using Razor
The string is going to end up looking like this:
https://www.notmysite/controller/action?order_ID=xxxxxxx&hashComparator=iFxp3%2BszAMaEB%2FnHCtRr9Ulhv0DumtyDumCik4gKypJqi0BdOGXXsr9QDkfefAsLaR1Xy%2BrX9VcuzP1pF2k6dL%2F92UxphzTKqNAZP2SSZGWtvyO5az%2F9JvDY%2Bsq5TBQo7%2FYAAMIU4QxiSX1SBKk8SUNECW3ZmKM%3D
In my model I have the order id and the hash string
As the route is not a part of my site I don't believe I can use the default methods like #Url.Action
and therefore can't use protocol: Request.Url.Scheme
like I've used elsewhere.
So at present I'm trying to figure out how to create this using string functions
I've tried
Url.Encode
Url.EscapeDataString
Html.Encode
but am getting no where fast:
Click Here to be transferred
The output text always has plusses and equals in them and doesn't work.
Which combination do I need?!
I've figured out a way of doing it:
#{
var url = string.Format(
"https://www.notmysite.co.uk/controller/action?order_ID={0}&hashComparator={1}",
#Uri.EscapeDataString(Model.bookingNumber.ToString()),
#Uri.EscapeDataString(Model.hashCode));
}
<p>Click Here to be transferred</p>
Edit 2015 - As mentioned by Jerads post - The solution is to only encode the query string elements and not the whole URL - which is what the above does.
This was the first link that came up for this issue for me. The answers didn't work for me though because I am using core, I think. So wanted to add this in.
System.Net.WebUtility.UrlEncode(MyVariableName)
If Url.Encode doesn't work try the above. Also as stated before don't use this on the entire URL string, just use it for the individual querystring variables. Otherwise there is a good chance your URL wont work.
The problem is that you're trying to encode the whole URL. The only pieces you want to encode are the querystring values, and you can just use Url.Encode() for this.
You don't want to encode the address, the querystring params, or the ? and & delimiters, otherwise you'll end up with an address the browser can't parse.
Ultimately, it would look something like this:
Click Here to be transferred
The easier method is to use #Html.Raw(Model.SomethingUrl)

URL routing issue when data have special symbol

I have developed a ASP.NET MVC application. I have a conroller with the name EmployeeController and it got a method called GetEmployeeByName. GetEmployeeByName() takes a name of type string as parameter.
So When I send a request like this, i get the data back :
someDomain:9999/Employee/GetEmployeeByName/Roger Federer
But if the name contains an '&' (you & me), I get a '400 Bad Request' as response from server.
someDomain:9999/Employee/GetEmployeeByName/you%20&%20me
Even if i encode it dont get a reposne back
someDomain:9999/Employee/GetEmployeeByName/you%20&%20me
What is the right way to encode such (data with special character) data?
What is the right way to encode such (data with special character) data?
The right way is to use a query string parameter and not be putting those things as part of the uri portion. Read the following blog post from Scott Hansleman. I will only quote hos conclusion:
After ALL this effort to get crazy stuff in the Request Path, it's
worth mentioning that simply keeping the values as a part of the Query
String (remember WAY back at the beginning of this post?) is easier,
cleaner, more flexible, and more secure.
As you can see in the blog post there are some hacky ways to make it work and circumvent IIS handling but it simply is not something that I would recommend you venturing into. Just put this name in the query string.

extract information from particular string

I am new to ruby on rails. I have passed parameters ISDCode,AreaCode and Telephone number using POST from a form.
I have a string with information of the format countryName(ISDCode) passed in the variable ISDCode. For example "United States of America(+1)".
Now I want to save only the value of the ISDCode in the database.
What would be the ideal way to extract the ISD Code from the string?
Should I extract the ISD Code in Javascript before user POSTs the form or should I extract it in the model using a callback ?
Also is regex the only way to extract the information?
Since the string is from auto completion, the ISDcodes should be existing in your database. So the best solution may be including an extra parameter (with a hidden input), like isdcode_id, then you simply use isdcode_id in your model. This way you can avoid the trouble to parse the string.
If this is not feasible, regex could be the best way to extract the information. You can override the setter in the model to do it.
use regular expression to match ISDcode
"United States of America(+1)" =~ /(\+[\d]+)/
puts $1
If you are interested in getting just the ISD Code alone, this should work:
"United States of America(+1)".gsub!(/[^\+\d]/, "")
NB: You can have this in your view helper and just call the helper on the string before persistence
Already answered, but I'd like to offer an alternative to getting the ISD Code:
isd = "United States(+1)"
puts isd[/[+]*[\d]{1,4}/] # +1
This regexp matches:
0001
+1
+01
etc.
I prefer to use js to extract information in the client side and make a validation in the model. By this way, you can get what you want and make sure it's correct.

Clean up & style characters from text

I am getting text from a feed that has alot of characters like:
Insignia&#153; 2.0 Stereo Computer Speaker System (2-Piece) - Black
4th-Generation AppleĀ® iPodĀ® touch
Is there an easy way to get rid of these, or do I have to anticipate which characters I want to delete and use the delete method to remove them? Also, when I try to remove
&
with
str.delete("&")
It leaves behind "amp;" Is there a better way to delete this type of character? Do I need to re-encode the text?
String#delete is certainly not what you want, as it works on characters, not the string as a whole.
Try
str.gsub /&/, ""
You may also want to try replacing the & with a literal ampersand, such as:
str.gsub /&/, "&"
If this is closer to what you really want, you may get the best results unescaping the HTML string. If so try this:
CGI::unescapeHTML(str)
Details of the unescapeHTML method are here.
If you are getting data from a 'feed', aka RSS XML, then you should be using an XML parser like Nokogiri to process the XML. This will automatically unescape HTML entities and allow you to get the proper string representation directly.
For removing try to use gsub method, something like this:
text = "foo&bar"
text.gsub /\b&\b/, "" #=> foobar

Find all URL's in a string in rails 3

I'm building an app in Rails 3 and I need a method to extract all urls from a string and store them in a hash or something. I know I need to use regular expressions but I don't know where exactly to begin with them.
Also, I know about auto_link, but it doesn't quite do what I'm trying to achieve. I just simply need a hash of all the url's from a string.
Thanks!
From http://www.regular-expressions.info/ruby.html
"To collect all regex matches in a string into an array, pass the regexp object to the string's scan() method, e.g.: myarray = mystring.scan(/regex/)."
So you probably need strings that start with "http". So check the docs for that :)
I don't program in Ruby and I'm not very good with regex but maybe this will help you out:
http://www.ozzu.com/programming-forum/url-regex-t104809.html

Resources