I have a web api built in .NET, within an endpoint i would like to redirect to an url which matches the the code inserted in database. the endpoint takes as entry the code and i am supposed to redirect to the corresponding url. For that i use the Redirect method which actually is not working. i did Console.Write to print if the url is null or empty but it exists. here is the code of my controller :
[HttpGet("{hash}")]
// [ProducesResponseType(302)]
//[ProducesResponseType(404)]
public async Task<IActionResult> redirectUrl(string hash)
{
var t = await new ServiceUrl(_ctx).GetTarget2(hash);
int a = 0;
foreach (Model.Data.DAO.Url i in t)
{
if (i != null)
{
a=a 1;
}
}
if (a==0)
{
return new TimeoutExceptionObjectResult(error: "Not Found",503);
}else
if (DateTime.Compare(DateTime.Now, t.ElementAt(0).ExpireAt) > 0)
{
t.ElementAt(0).state = "expire";
_ctx.Entry(t.ElementAt(0)).State = EntityState.Modified;
await _ctx.SaveChangesAsync();
return new TimeoutExceptionObjectResult(error: "Url expiré",501);
}
string url= t.ElementAt(0).UrlOrigin;
Console.Write(url);
return new Redirect(url);
}
the GetTarget2 method :
public async Task<IEnumerable<Url>> GetTarget2(string hash)
{
var t2 = await _ctx.Url.Where(u => u.UrlShort == hash).ToArrayAsync();
return t2;
}
and the entity :
[Table("Url")]
public class Url
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string UrlShort { get; set; }
public string UrlOrigin { get; set; }
public string state { get; set; }
public int Customer_id { get; set; }
public int? targetItemId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ExpireAt { get; set; }
[JsonIgnore]
public List<Stats> GetStats { get; set; }
public Url()
{
}
public Url(int Id,string UrlShort,string UrlOrigin,string state,int Customer_id,DateTime CreatedAt,DateTime ExpireAt)
{
this.Id = Id;
this.UrlShort = UrlShort;
this.UrlOrigin = UrlOrigin;
this.state = state;
this.Customer_id = Customer_id;
this.CreatedAt = CreatedAt;
this.ExpireAt = ExpireAt;
}
}
when i try to pass a code which is in database i get this : Not found
which means it does not find it in database
Update: the database context declaration :
private readonly DatabaseContext _ctx;
and its definition :
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public DbSet<Url> Url { get; set; }
public DbSet<User> User { get; set; }
public DbSet<Stats> Stats { get; set; }
}
CodePudding user response:
If I understood your question correctly then you are trying to redirect to another action method by reading the URL from the database. If the retured Url belongs to your application and just have the dynamic parameter then you can try RedirectToAction method for the redirect.
Syntax:
return RedirectToAction("ActionName", new { id = 90 });
The will cause a redirect to YourApp/Controller/ActionName/90
All you have to create an action method with argument (if required) and redirect from another action method from where you are getting url.
Alternativly you can also use
return RedirectToAction("Index", new RouteValueDictionary(
new { controller = "NameOfController", action = "Index", id = id } )
);
CodePudding user response:
The problem was the Redirect method which indicates its content was null or empty although i printed the url in terminal successfully. So What i did is adding a prefix (http://) like this Redirect("http://" url), url is the url coming from the database. if the url already contains the prefix http or https i remove it before passing it as parameter inside Redirect method. Doing this solved my problem.