0

I'm trying to build a function within my mvc app that will validate the user's url input. in my code below, i get the "not all code paths return a value." I need help figuring out why it doesn't like when i return result. thanks!

public static long InsertUrl(string inputUrl)
        {
            long result = 0;
            if (!string.IsNullOrEmpty(inputUrl))
            {

                using (ShortUrlEntities db = new ShortUrlEntities())
                {

                    if (inputUrl.IndexOf(@"://test/") == -1)
                    {
                        inputUrl = "http://test/" + inputUrl;
                    }

                    Regex RgxUrl = new Regex("(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?");
                    if (RgxUrl.IsMatch(inputUrl))
                    {
                        ShortURL su = new ShortURL();
                        su.url = inputUrl;
                        db.AddToShortURLSet(su);
                        db.SaveChanges();
                        result = su.id;
                    }

                    return result;
                }
            }

        }
    }
}

2 Answers 2

1

You have an If

if (!string.IsNullOrEmpty(inputUrl))

and your return is inside that if. In the event that the inputUrl is null or empty, you are not returning anything.

Move your return Result; to outside the if.

Sign up to request clarification or add additional context in comments.

1 Comment

too easy, i guess i had been looking at the code too long. Thanks!
0

The problem is that if this:

if (!string.IsNullOrEmpty(inputUrl))

fails then no result will ever be returned. This is needed for all function that return a value not just in MVC.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.