简体   繁体   中英

can I use a c# switch here?

i would like to refactor this code. Maybe if possible by using a switch? Or is it the same in terms of performance?

string rawUrl = context.Request.RawUrl ?? string.Empty;

if (rawUrl.Contains("mypage.aspx"))
{
}

if (rawUrl.Contains("mypage2.aspx"))
{
}

etc..

Not directly, since you want a "contains" relation, rather than an exact equality.

However, if you so desire, you could do it indirectly by attempting to parse the page name out of what I assume would be the URL, storing it in a separate String variable, and switching on that String .

For example:

// Get the URL from some external source (wherever you're already getting it from)
String rawUrl = "http://www.example.com/foo/bar.aspx";

// Means of parsing will be dependent on the format in which you expect the URL.
String page = rawUrl.Substring(rawUrl.LastIndexOf("/") + 1);

switch (page) {
    case "bar.aspx":
        // Do stuff
        break;
    case "foo.aspx":
        // Do stuff
        break;
}

And, of course, please take this parsing methodology with a grain of salt; this example was to show you that it is possible, but note that this method of parsing could potentially throw an exception in a number of cases, but I've omitted those checks for the sake of brevity.

Switch Cases must be a constant value. You're best bet there is to use if/else like so:

string rawUrl = context.Request.RawUrl ?? string.Empty;

if (rawUrl.Contains("mypage.aspx"))
{
    //code
}
else if (rawUrl.Contains("mypage2.aspx"))
{
    //more code
}

If you're concerned about performance (which is good!) then the else is the way to go. While not using an else will have the same functionality, by adding the else, you're telling the code to not process any of the other if conditions. So 10 if statements will result in 10 if conditions being processed not matter what, while 10 if/else statements might result in 10, or it might only result in 1.

EDIT:

Thought about this some, and I noticed you were using the context object. If you really wanted a switch statement, you can do the following:

string page = context.Request.Url.Segments.Last();

switch(page)
{
    case "mypage.aspx":
        //code
        break;
    case "mypage2.aspx":
        //more code
        break;
}

Not for a contains.

Try to isolate page name alone and you can could do it.

switch(pageName)
{
    case "mypage.aspx";
        break;

    case "mypage2.aspx";
        break;
}

I think it is better to use a Dictionary .

First, extract the file name from the raw url.

Then, use a Dictionary<string,TValue> .

If the actions to the pages are almost the same, set TValue to the type of the data associated with the pages.

If the actions are very different, set TValue to a delegate type such as Action .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM