Reputation: 893
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;
}
}
}
}
}
Upvotes: 0
Views: 811
Reputation: 266
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.
Upvotes: 0
Reputation: 23093
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.
Upvotes: 1