我正在做 URL 缩短服务。但目前我面临一个问题。当我尝试输入 URL 时,例如:
http://localhost:4021/书/文章
它应该将我重定向到长 URL,例如:
但是当我输入缩短的 URL 时,它无法重定向。
以下是我的代码。怎么了?
string query;
string name = Page.RouteData.Values["Name"] as string;
string str = "Data Source=blankblankblank;Initial Catalog=URLSHORT;User ID=blank;Password=blank";
SqlConnection con = new SqlConnection(str);
con.Open();
query = "SELECT @long_url FROM dbo.url_map WHERE @short_url='" + name + "'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("short_url", SqlDbType.VarChar).Value = tbShort.Text;
cmd.Parameters.Add("long_url", SqlDbType.VarChar).Value = tbLong.Text;
cmd.CommandText = string.Format("SELECT @long_url FROM dbo.url_map WHERE @short_url='" + name + "'");
if(name != null)
{
else if (name == cmd.ExecuteNonQuery().ToString())
{
Response.StatusCode = 303;
Response.Status = "303 Moved Permanently";
Response.RedirectLocation = "http://www.youtube.com";
Response.End();
}