Parsing and URL string coming from database in MVC C#.

Here in the code splittting the string and find if its legnth is greater than 2 then gets the controller and action name and redirects to right action method. Is there any better way to parse the db string ?

String abc = "/A/B/C.asp?Target=Z"; // getting this mvc string url from db
var target = abc.Split('/');
if (target.Length < 2) throw new Exception("unable to find link - Link: " + abc);
var controller = target[target.Length - 2];
var action = target[target.Length - 1];
return RedirectToAction(action, controller, menuModel);
else
//do something else

Open in new window

satmishaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
Why not? It not bad method, if we speak only about parse string. May be you can optimization information, which you store in database,.
0
Fernando SotoRetiredCommented:
Hi satmisha;

How about using a regular expression to parse the string. Something like this.
using System.Text.RegularExpressions;

// Regex pattern to match the input with
var pattern = "/.+/(?<Controller>.+)/(?<Action>.+)";
// The data from the database
var input = "/A/B/C.asp?Target=Z";
// Find the controller and action
Match results = Regex.Match(input, pattern);
// If a match was found
if(results.Success)
{
    // Access the Controller and Action values
    Console.WriteLine("The Controller : {0}\nThe Action : {1}", results.Groups["Controller"], results.Groups["Action"]);
}
else
{
    // If not found throw an exception
    throw new Exception("unable to find link - Link: " + input);
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Miguel OzSoftware EngineerCommented:
I agree with Misha, rather than saving a url just save the controller and action to the database , thus no need to do extra parsing.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

satmishaAuthor Commented:
thanx experts.

I am actually getting the url from db as I mentioned and something it redirects to mvc controller and action and sometimes to .aspx or .asp pages.


String abc = "/A/B/C.asp?Target=Z"; // getting this mvc string url from db.
0
Fernando SotoRetiredCommented:
Hi satmisha;

Please try my posted solution above, it will do what you need.
0
satmishaAuthor Commented:
Thanks Fernando Soto
0
satmishaAuthor Commented:
Thanks Pal.
0
Fernando SotoRetiredCommented:
Not a problem satmisha, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.